]> git.ipfire.org Git - thirdparty/kmod.git/blame - libkmod/libkmod-config.c
util: add helper function to compare timestamps
[thirdparty/kmod.git] / libkmod / libkmod-config.c
CommitLineData
7c2ab358
LDM
1/*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011 ProFUSION embedded systems
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
cb451f35
LDM
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
7c2ab358
LDM
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
7c2ab358
LDM
36struct kmod_alias {
37 char *name;
43c29d10 38 char modname[];
7c2ab358
LDM
39};
40
615c42be
LDM
41struct kmod_options {
42 char *options;
43 char modname[];
44};
45
a5cce6d6
LDM
46struct kmod_command {
47 char *command;
48 char modname[];
49};
50
1c522600
GSB
51struct 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
c1c9c446
LDM
59const char *kmod_blacklist_get_modname(const struct kmod_list *l)
60{
61 return l->data;
62}
63
b0ef19f7 64const char *kmod_alias_get_name(const struct kmod_list *l) {
1ce08a56 65 const struct kmod_alias *alias = l->data;
b0ef19f7
LDM
66 return alias->name;
67}
68
69const char *kmod_alias_get_modname(const struct kmod_list *l) {
1ce08a56 70 const struct kmod_alias *alias = l->data;
b0ef19f7
LDM
71 return alias->modname;
72}
73
bd3f5535
GSB
74const char *kmod_option_get_options(const struct kmod_list *l) {
75 const struct kmod_options *alias = l->data;
76 return alias->options;
77}
78
79const char *kmod_option_get_modname(const struct kmod_list *l) {
80 const struct kmod_options *alias = l->data;
81 return alias->modname;
82}
83
84const char *kmod_command_get_command(const struct kmod_list *l) {
85 const struct kmod_command *alias = l->data;
86 return alias->command;
87}
88
89const char *kmod_command_get_modname(const struct kmod_list *l) {
90 const struct kmod_command *alias = l->data;
91 return alias->modname;
92}
93
1c522600
GSB
94const char *kmod_softdep_get_name(const struct kmod_list *l) {
95 const struct kmod_softdep *dep = l->data;
96 return dep->name;
97}
98
99const 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
105const 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
a5cce6d6
LDM
111static 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
e5a7f6ac 122 DBG(config->ctx, "modname='%s' cmd='%s %s'\n", modname, command_name,
a5cce6d6
LDM
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
140oom_error:
141 free(cmd);
142oom_error_init:
143 ERR(config->ctx, "out-of-memory\n");
144 return -ENOMEM;
145}
146
147static 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
615c42be
LDM
157static 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
23c0d012 165 DBG(config->ctx, "modname='%s' options='%s'\n", modname, options);
615c42be
LDM
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
184oom_error:
185 free(opt);
186oom_error_init:
187 ERR(config->ctx, "out-of-memory\n");
188 return -ENOMEM;
189}
190
c35347f1
LDM
191static void kmod_config_free_options(struct kmod_config *config,
192 struct kmod_list *l)
615c42be
LDM
193{
194 struct kmod_options *opt = l->data;
195
196 free(opt);
197
198 config->options = kmod_list_remove(l);
199}
200
d13e606f 201static int kmod_config_add_alias(struct kmod_config *config,
c35347f1 202 const char *name, const char *modname)
7c2ab358
LDM
203{
204 struct kmod_alias *alias;
d13e606f 205 struct kmod_list *list;
43c29d10 206 size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;
7c2ab358 207
d13e606f 208 DBG(config->ctx, "name=%s modname=%s\n", name, modname);
7c2ab358 209
43c29d10 210 alias = malloc(sizeof(*alias) + namelen + modnamelen);
d13e606f
GSB
211 if (!alias)
212 goto oom_error_init;
28c175ed 213
43c29d10
GSB
214 alias->name = sizeof(*alias) + modnamelen + (char *)alias;
215
216 memcpy(alias->modname, modname, modnamelen);
217 memcpy(alias->name, name, namelen);
7c2ab358 218
d13e606f
GSB
219 list = kmod_list_append(config->aliases, alias);
220 if (!list)
221 goto oom_error;
28c175ed 222
d13e606f
GSB
223 config->aliases = list;
224 return 0;
225
226oom_error:
d13e606f
GSB
227 free(alias);
228oom_error_init:
229 ERR(config->ctx, "out-of-memory name=%s modname=%s\n", name, modname);
230 return -ENOMEM;
7c2ab358
LDM
231}
232
c35347f1
LDM
233static void kmod_config_free_alias(struct kmod_config *config,
234 struct kmod_list *l)
7c2ab358
LDM
235{
236 struct kmod_alias *alias = l->data;
237
7c2ab358
LDM
238 free(alias);
239
d13e606f 240 config->aliases = kmod_list_remove(l);
7c2ab358
LDM
241}
242
d13e606f 243static int kmod_config_add_blacklist(struct kmod_config *config,
c35347f1 244 const char *modname)
81cf2060 245{
81cf2060 246 char *p;
d13e606f 247 struct kmod_list *list;
81cf2060 248
d13e606f 249 DBG(config->ctx, "modname=%s\n", modname);
81cf2060
LDM
250
251 p = strdup(modname);
d13e606f
GSB
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;
81cf2060 260
d13e606f
GSB
261oom_error:
262 free(p);
263oom_error_init:
264 ERR(config->ctx, "out-of-memory modname=%s\n", modname);
265 return -ENOMEM;
81cf2060
LDM
266}
267
d13e606f 268static void kmod_config_free_blacklist(struct kmod_config *config,
81cf2060
LDM
269 struct kmod_list *l)
270{
271 free(l->data);
d13e606f 272 config->blacklists = kmod_list_remove(l);
81cf2060
LDM
273}
274
1c522600
GSB
275static 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
411static void kmod_config_free_softdep(struct kmod_config *config,
412 struct kmod_list *l)
413{
414 free(l->data);
415 config->softdeps = kmod_list_remove(l);
416}
417
1684e440
LDM
418static void kcmdline_parse_result(struct kmod_config *config, char *modname,
419 char *param, char *value)
420{
421 if (modname == NULL || param == NULL || value == NULL)
422 return;
423
424 DBG(config->ctx, "%s %s\n", modname, param);
425
426 if (streq(modname, "modprobe") && !strncmp(param, "blacklist=", 10)) {
427 for (;;) {
428 char *t = strsep(&value, ",");
429 if (t == NULL)
430 break;
431
432 kmod_config_add_blacklist(config, t);
433 }
434 } else {
435 kmod_config_add_options(config,
436 underscores(config->ctx, modname), param);
437 }
438}
439
440static int kmod_config_parse_kcmdline(struct kmod_config *config)
441{
442 char buf[KCMD_LINE_SIZE];
443 int fd, err;
444 char *p, *modname, *param = NULL, *value = NULL;
445
79e5ea91 446 fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC);
1684e440
LDM
447 err = read_str_safe(fd, buf, sizeof(buf));
448 close(fd);
449 if (err < 0) {
450 ERR(config->ctx, "could not read from '/proc/cmdline': %s\n",
451 strerror(-err));
452 return err;
453 }
454
455 for (p = buf, modname = buf; *p != '\0' && *p != '\n'; p++) {
456 switch (*p) {
457 case ' ':
458 *p = '\0';
459 kcmdline_parse_result(config, modname, param, value);
460 param = value = NULL;
461 modname = p + 1;
462 break;
463 case '.':
464 *p = '\0';
465 param = p + 1;
466 break;
467 case '=':
135bffd6
LDM
468 if (param != NULL)
469 value = p + 1;
1684e440
LDM
470 break;
471 }
472 }
473
474 *p = '\0';
475 kcmdline_parse_result(config, modname, param, value);
476
477 return 0;
478}
479
b7b7ac29
LDM
480/*
481 * Take an fd and own it. It will be closed on return. filename is used only
482 * for debug messages
483 */
484static int kmod_config_parse(struct kmod_config *config, int fd,
485 const char *filename)
7c2ab358 486{
d13e606f 487 struct kmod_ctx *ctx = config->ctx;
7c2ab358
LDM
488 char *line;
489 FILE *fp;
759214fa 490 unsigned int linenum = 0;
b7b7ac29 491 int err;
7c2ab358 492
b7b7ac29
LDM
493 fp = fdopen(fd, "r");
494 if (fp == NULL) {
495 err = -errno;
496 ERR(config->ctx, "fd %d: %m", fd);
497 close(fd);
498 return err;
499 }
7c2ab358
LDM
500
501 while ((line = getline_wrapped(fp, &linenum)) != NULL) {
c11e62bf 502 char *cmd, *saveptr;
7c2ab358
LDM
503
504 if (line[0] == '\0' || line[0] == '#')
505 goto done_next;
506
c11e62bf 507 cmd = strtok_r(line, "\t ", &saveptr);
7c2ab358
LDM
508 if (cmd == NULL)
509 goto done_next;
510
877e80cd 511 if (streq(cmd, "alias")) {
c11e62bf
LDM
512 char *alias = strtok_r(NULL, "\t ", &saveptr);
513 char *modname = strtok_r(NULL, "\t ", &saveptr);
7c2ab358
LDM
514
515 if (alias == NULL || modname == NULL)
516 goto syntax_error;
517
d13e606f 518 kmod_config_add_alias(config,
30be7513
LDM
519 underscores(ctx, alias),
520 underscores(ctx, modname));
877e80cd 521 } else if (streq(cmd, "blacklist")) {
c11e62bf 522 char *modname = strtok_r(NULL, "\t ", &saveptr);
81cf2060
LDM
523
524 if (modname == NULL)
525 goto syntax_error;
526
d13e606f 527 kmod_config_add_blacklist(config,
2295acc5 528 underscores(ctx, modname));
615c42be
LDM
529 } else if (streq(cmd, "options")) {
530 char *modname = strtok_r(NULL, "\t ", &saveptr);
531
532 if (modname == NULL)
533 goto syntax_error;
534
535 kmod_config_add_options(config,
536 underscores(ctx, modname),
537 strtok_r(NULL, "\0", &saveptr));
40ee8dad 538 } else if (streq(cmd, "install")) {
a5cce6d6
LDM
539 char *modname = strtok_r(NULL, "\t ", &saveptr);
540
541 if (modname == NULL)
542 goto syntax_error;
543
544 kmod_config_add_command(config,
545 underscores(ctx, modname),
546 strtok_r(NULL, "\0", &saveptr),
547 cmd, &config->install_commands);
40ee8dad 548 } else if (streq(cmd, "remove")) {
a5cce6d6
LDM
549 char *modname = strtok_r(NULL, "\t ", &saveptr);
550
551 if (modname == NULL)
552 goto syntax_error;
553
554 kmod_config_add_command(config,
555 underscores(ctx, modname),
556 strtok_r(NULL, "\0", &saveptr),
557 cmd, &config->remove_commands);
40ee8dad 558 } else if (streq(cmd, "softdep")) {
1c522600
GSB
559 char *modname = strtok_r(NULL, "\t ", &saveptr);
560
561 if (modname == NULL)
562 goto syntax_error;
563
564 kmod_config_add_softdep(config,
565 underscores(ctx, modname),
566 strtok_r(NULL, "\0", &saveptr));
615c42be 567 } else if (streq(cmd, "include")
877e80cd 568 || streq(cmd, "config")) {
81cf2060
LDM
569 INFO(ctx, "%s: command %s not implemented yet\n",
570 filename, cmd);
7c2ab358
LDM
571 } else {
572syntax_error:
573 ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n",
574 filename, linenum, cmd);
575 }
576
577done_next:
578 free(line);
579 }
580
581 fclose(fp);
582
583 return 0;
584}
585
d13e606f 586void kmod_config_free(struct kmod_config *config)
7c2ab358
LDM
587{
588 while (config->aliases)
d13e606f 589 kmod_config_free_alias(config, config->aliases);
81cf2060
LDM
590
591 while (config->blacklists)
d13e606f
GSB
592 kmod_config_free_blacklist(config, config->blacklists);
593
615c42be
LDM
594 while (config->options)
595 kmod_config_free_options(config, config->options);
596
a5cce6d6
LDM
597 while (config->install_commands) {
598 kmod_config_free_command(config, config->install_commands,
599 &config->install_commands);
600 }
601
602 while (config->remove_commands) {
603 kmod_config_free_command(config, config->remove_commands,
604 &config->remove_commands);
605 }
606
1c522600
GSB
607 while (config->softdeps)
608 kmod_config_free_softdep(config, config->softdeps);
609
d13e606f 610 free(config);
7c2ab358
LDM
611}
612
98c80f44
LDM
613static bool conf_files_filter_out(struct kmod_ctx *ctx, DIR *d,
614 const char *path, const char *fn)
7c2ab358
LDM
615{
616 size_t len = strlen(fn);
98c80f44 617 struct stat st;
7c2ab358
LDM
618
619 if (fn[0] == '.')
8f767e2d 620 return true;
7c2ab358 621
877e80cd 622 if (len < 6 || (!streq(&fn[len - 5], ".conf")
9070b117 623 && !streq(&fn[len - 6], ".alias")))
8f767e2d 624 return true;
7c2ab358 625
98c80f44
LDM
626 fstatat(dirfd(d), fn, &st, 0);
627
628 if (S_ISDIR(st.st_mode)) {
629 ERR(ctx, "Directories inside directories are not supported: "
630 "%s/%s\n", path, fn);
8f767e2d 631 return true;
98c80f44
LDM
632 }
633
8f767e2d 634 return false;
7c2ab358
LDM
635}
636
7fe5f7ab
LDM
637struct conf_file {
638 const char *path;
639 bool is_single;
640 char name[];
641};
642
643static int conf_files_insert_sorted(struct kmod_ctx *ctx,
644 struct kmod_list **list,
645 const char *path, const char *name)
646{
647 struct kmod_list *lpos, *tmp;
648 struct conf_file *cf;
649 size_t namelen;
650 int cmp = -1;
651 bool is_single = false;
652
653 if (name == NULL) {
654 name = basename(path);
655 is_single = true;
656 }
657
658 kmod_list_foreach(lpos, *list) {
659 cf = lpos->data;
660
661 if ((cmp = strcmp(name, cf->name)) <= 0)
662 break;
663 }
664
665 if (cmp == 0) {
666 DBG(ctx, "Ignoring duplicate config file: %s/%s\n", path,
667 name);
668 return -EEXIST;
669 }
670
671 namelen = strlen(name);
672 cf = malloc(sizeof(*cf) + namelen + 1);
673 if (cf == NULL)
674 return -ENOMEM;
675
676 memcpy(cf->name, name, namelen + 1);
677 cf->path = path;
678 cf->is_single = is_single;
679
680 if (lpos == NULL)
681 tmp = kmod_list_append(*list, cf);
682 else if (lpos == *list)
683 tmp = kmod_list_prepend(*list, cf);
684 else
685 tmp = kmod_list_insert_before(lpos, cf);
686
687 if (tmp == NULL) {
688 free(cf);
689 return -ENOMEM;
690 }
691
692 if (lpos == NULL || lpos == *list)
693 *list = tmp;
694
695 return 0;
696}
697
4782396c 698/*
7fe5f7ab 699 * Insert configuration files in @list, ignoring duplicates
4782396c 700 */
7fe5f7ab 701static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
b7b7ac29 702 const char *path)
7c2ab358 703{
7c2ab358
LDM
704 DIR *d;
705 int err;
7fe5f7ab 706 struct stat st;
7c2ab358 707
7fe5f7ab
LDM
708 if (stat(path, &st) != 0) {
709 err = -errno;
710 DBG(ctx, "could not stat '%s': %m\n", path);
711 return err;
712 }
713
714 if (S_ISREG(st.st_mode)) {
715 conf_files_insert_sorted(ctx, list, path, NULL);
716 return 0;
717 } if (!S_ISDIR(st.st_mode)) {
718 ERR(ctx, "unsupported file mode %s: %#x\n",
719 path, st.st_mode);
720 return -EINVAL;
721 }
1c250ec1 722
7c2ab358
LDM
723 d = opendir(path);
724 if (d == NULL) {
7c2ab358 725 ERR(ctx, "%m\n");
7fe5f7ab 726 return -EINVAL;
7c2ab358
LDM
727 }
728
729 for (;;) {
730 struct dirent ent, *entp;
7c2ab358
LDM
731
732 err = readdir_r(d, &ent, &entp);
733 if (err != 0) {
b7b7ac29
LDM
734 ERR(ctx, "reading entry %s\n", strerror(-err));
735 goto fail_read;
7c2ab358
LDM
736 }
737
738 if (entp == NULL)
739 break;
740
8f767e2d 741 if (conf_files_filter_out(ctx, d, path, entp->d_name))
7c2ab358
LDM
742 continue;
743
7fe5f7ab 744 conf_files_insert_sorted(ctx, list, path, entp->d_name);
b7b7ac29 745 }
7c2ab358 746
7fe5f7ab
LDM
747 closedir(d);
748 return 0;
7c2ab358 749
b7b7ac29 750fail_read:
b7b7ac29 751 closedir(d);
7fe5f7ab 752 return err;
7c2ab358
LDM
753}
754
c35347f1
LDM
755int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config,
756 const char * const *config_paths)
7c2ab358 757{
d13e606f 758 struct kmod_config *config;
7fe5f7ab 759 struct kmod_list *list = NULL;
b7b7ac29 760 size_t i;
7c2ab358 761
d13e606f 762 *p_config = config = calloc(1, sizeof(struct kmod_config));
2295acc5 763 if (config == NULL)
d13e606f 764 return -ENOMEM;
2295acc5 765
d13e606f
GSB
766 config->ctx = ctx;
767
cb8d4d3e
GSB
768 for (i = 0; config_paths[i] != NULL; i++) {
769 const char *path = config_paths[i];
7c2ab358 770
7fe5f7ab
LDM
771 conf_files_list(ctx, &list, path);
772 }
cb8d4d3e 773
7fe5f7ab
LDM
774 for (; list != NULL; list = kmod_list_remove(list)) {
775 char fn[PATH_MAX];
776 struct conf_file *cf = list->data;
777 int fd;
cb8d4d3e 778
7fe5f7ab
LDM
779 if (cf->is_single)
780 strcpy(fn, cf->path);
781 else
782 snprintf(fn, sizeof(fn),"%s/%s", cf->path,
783 cf->name);
7c2ab358 784
7fe5f7ab
LDM
785 fd = open(fn, O_RDONLY|O_CLOEXEC);
786 DBG(ctx, "parsing file '%s' fd=%d\n", fn, fd);
7c2ab358 787
7fe5f7ab
LDM
788 if (fd >= 0)
789 kmod_config_parse(config, fd, fn);
7c2ab358 790
7fe5f7ab 791 free(cf);
7c2ab358
LDM
792 }
793
1684e440
LDM
794 kmod_config_parse_kcmdline(config);
795
b7b7ac29 796 return 0;
7c2ab358 797}