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