]> git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-config.c
a5c6d114eb247f26dccc04405b2c6200618e7f27
[thirdparty/kmod.git] / libkmod / libkmod-config.c
1 /*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011-2012 ProFUSION embedded systems
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <stdarg.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32
33 #include "libkmod.h"
34 #include "libkmod-private.h"
35
36 struct kmod_alias {
37 char *name;
38 char modname[];
39 };
40
41 struct kmod_options {
42 char *options;
43 char modname[];
44 };
45
46 struct kmod_command {
47 char *command;
48 char modname[];
49 };
50
51 struct kmod_softdep {
52 char *name;
53 const char **pre;
54 const char **post;
55 unsigned int n_pre;
56 unsigned int n_post;
57 };
58
59 const char *kmod_blacklist_get_modname(const struct kmod_list *l)
60 {
61 return l->data;
62 }
63
64 const char *kmod_alias_get_name(const struct kmod_list *l) {
65 const struct kmod_alias *alias = l->data;
66 return alias->name;
67 }
68
69 const char *kmod_alias_get_modname(const struct kmod_list *l) {
70 const struct kmod_alias *alias = l->data;
71 return alias->modname;
72 }
73
74 const char *kmod_option_get_options(const struct kmod_list *l) {
75 const struct kmod_options *alias = l->data;
76 return alias->options;
77 }
78
79 const char *kmod_option_get_modname(const struct kmod_list *l) {
80 const struct kmod_options *alias = l->data;
81 return alias->modname;
82 }
83
84 const char *kmod_command_get_command(const struct kmod_list *l) {
85 const struct kmod_command *alias = l->data;
86 return alias->command;
87 }
88
89 const char *kmod_command_get_modname(const struct kmod_list *l) {
90 const struct kmod_command *alias = l->data;
91 return alias->modname;
92 }
93
94 const char *kmod_softdep_get_name(const struct kmod_list *l) {
95 const struct kmod_softdep *dep = l->data;
96 return dep->name;
97 }
98
99 const char * const *kmod_softdep_get_pre(const struct kmod_list *l, unsigned int *count) {
100 const struct kmod_softdep *dep = l->data;
101 *count = dep->n_pre;
102 return dep->pre;
103 }
104
105 const char * const *kmod_softdep_get_post(const struct kmod_list *l, unsigned int *count) {
106 const struct kmod_softdep *dep = l->data;
107 *count = dep->n_post;
108 return dep->post;
109 }
110
111 static int kmod_config_add_command(struct kmod_config *config,
112 const char *modname,
113 const char *command,
114 const char *command_name,
115 struct kmod_list **list)
116 {
117 struct kmod_command *cmd;
118 struct kmod_list *l;
119 size_t modnamelen = strlen(modname) + 1;
120 size_t commandlen = strlen(command) + 1;
121
122 DBG(config->ctx, "modname='%s' cmd='%s %s'\n", modname, command_name,
123 command);
124
125 cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
126 if (cmd == NULL)
127 goto oom_error_init;
128
129 cmd->command = sizeof(*cmd) + modnamelen + (char *)cmd;
130 memcpy(cmd->modname, modname, modnamelen);
131 memcpy(cmd->command, command, commandlen);
132
133 l = kmod_list_append(*list, cmd);
134 if (l == NULL)
135 goto oom_error;
136
137 *list = l;
138 return 0;
139
140 oom_error:
141 free(cmd);
142 oom_error_init:
143 ERR(config->ctx, "out-of-memory\n");
144 return -ENOMEM;
145 }
146
147 static void kmod_config_free_command(struct kmod_config *config,
148 struct kmod_list *l,
149 struct kmod_list **list)
150 {
151 struct kmod_command *cmd = l->data;
152
153 free(cmd);
154 *list = kmod_list_remove(l);
155 }
156
157 static int kmod_config_add_options(struct kmod_config *config,
158 const char *modname, const char *options)
159 {
160 struct kmod_options *opt;
161 struct kmod_list *list;
162 size_t modnamelen = strlen(modname) + 1;
163 size_t optionslen = strlen(options) + 1;
164
165 DBG(config->ctx, "modname='%s' options='%s'\n", modname, options);
166
167 opt = malloc(sizeof(*opt) + modnamelen + optionslen);
168 if (opt == NULL)
169 goto oom_error_init;
170
171 opt->options = sizeof(*opt) + modnamelen + (char *)opt;
172
173 memcpy(opt->modname, modname, modnamelen);
174 memcpy(opt->options, options, optionslen);
175 strchr_replace(opt->options, '\t', ' ');
176
177 list = kmod_list_append(config->options, opt);
178 if (list == NULL)
179 goto oom_error;
180
181 config->options = list;
182 return 0;
183
184 oom_error:
185 free(opt);
186 oom_error_init:
187 ERR(config->ctx, "out-of-memory\n");
188 return -ENOMEM;
189 }
190
191 static void kmod_config_free_options(struct kmod_config *config,
192 struct kmod_list *l)
193 {
194 struct kmod_options *opt = l->data;
195
196 free(opt);
197
198 config->options = kmod_list_remove(l);
199 }
200
201 static int kmod_config_add_alias(struct kmod_config *config,
202 const char *name, const char *modname)
203 {
204 struct kmod_alias *alias;
205 struct kmod_list *list;
206 size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;
207
208 DBG(config->ctx, "name=%s modname=%s\n", name, modname);
209
210 alias = malloc(sizeof(*alias) + namelen + modnamelen);
211 if (!alias)
212 goto oom_error_init;
213
214 alias->name = sizeof(*alias) + modnamelen + (char *)alias;
215
216 memcpy(alias->modname, modname, modnamelen);
217 memcpy(alias->name, name, namelen);
218
219 list = kmod_list_append(config->aliases, alias);
220 if (!list)
221 goto oom_error;
222
223 config->aliases = list;
224 return 0;
225
226 oom_error:
227 free(alias);
228 oom_error_init:
229 ERR(config->ctx, "out-of-memory name=%s modname=%s\n", name, modname);
230 return -ENOMEM;
231 }
232
233 static void kmod_config_free_alias(struct kmod_config *config,
234 struct kmod_list *l)
235 {
236 struct kmod_alias *alias = l->data;
237
238 free(alias);
239
240 config->aliases = kmod_list_remove(l);
241 }
242
243 static int kmod_config_add_blacklist(struct kmod_config *config,
244 const char *modname)
245 {
246 char *p;
247 struct kmod_list *list;
248
249 DBG(config->ctx, "modname=%s\n", modname);
250
251 p = strdup(modname);
252 if (!p)
253 goto oom_error_init;
254
255 list = kmod_list_append(config->blacklists, p);
256 if (!list)
257 goto oom_error;
258 config->blacklists = list;
259 return 0;
260
261 oom_error:
262 free(p);
263 oom_error_init:
264 ERR(config->ctx, "out-of-memory modname=%s\n", modname);
265 return -ENOMEM;
266 }
267
268 static void kmod_config_free_blacklist(struct kmod_config *config,
269 struct kmod_list *l)
270 {
271 free(l->data);
272 config->blacklists = kmod_list_remove(l);
273 }
274
275 static int kmod_config_add_softdep(struct kmod_config *config,
276 const char *modname,
277 const char *line)
278 {
279 struct kmod_list *list;
280 struct kmod_softdep *dep;
281 const char *s, *p;
282 char *itr;
283 unsigned int n_pre = 0, n_post = 0;
284 size_t modnamelen = strlen(modname) + 1;
285 size_t buflen = 0;
286 bool was_space = false;
287 enum { S_NONE, S_PRE, S_POST } mode = S_NONE;
288
289 DBG(config->ctx, "modname=%s\n", modname);
290
291 /* analyze and count */
292 for (p = s = line; ; s++) {
293 size_t plen;
294
295 if (*s != '\0') {
296 if (!isspace(*s)) {
297 was_space = false;
298 continue;
299 }
300
301 if (was_space) {
302 p = s + 1;
303 continue;
304 }
305 was_space = true;
306
307 if (p >= s)
308 continue;
309 }
310 plen = s - p;
311
312 if (plen == sizeof("pre:") - 1 &&
313 memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
314 mode = S_PRE;
315 else if (plen == sizeof("post:") - 1 &&
316 memcmp(p, "post:", sizeof("post:") - 1) == 0)
317 mode = S_POST;
318 else if (*s != '\0' || (*s == '\0' && !was_space)) {
319 if (mode == S_PRE) {
320 buflen += plen + 1;
321 n_pre++;
322 } else if (mode == S_POST) {
323 buflen += plen + 1;
324 n_post++;
325 }
326 }
327 p = s + 1;
328 if (*s == '\0')
329 break;
330 }
331
332 DBG(config->ctx, "%u pre, %u post\n", n_pre, n_post);
333
334 dep = malloc(sizeof(struct kmod_softdep) + modnamelen +
335 n_pre * sizeof(const char *) +
336 n_post * sizeof(const char *) +
337 buflen);
338 if (dep == NULL) {
339 ERR(config->ctx, "out-of-memory modname=%s\n", modname);
340 return -ENOMEM;
341 }
342 dep->n_pre = n_pre;
343 dep->n_post = n_post;
344 dep->pre = (const char **)((char *)dep + sizeof(struct kmod_softdep));
345 dep->post = dep->pre + n_pre;
346 dep->name = (char *)(dep->post + n_post);
347
348 memcpy(dep->name, modname, modnamelen);
349
350 /* copy strings */
351 itr = dep->name + modnamelen;
352 n_pre = 0;
353 n_post = 0;
354 mode = S_NONE;
355 for (p = s = line; ; s++) {
356 size_t plen;
357
358 if (*s != '\0') {
359 if (!isspace(*s)) {
360 was_space = false;
361 continue;
362 }
363
364 if (was_space) {
365 p = s + 1;
366 continue;
367 }
368 was_space = true;
369
370 if (p >= s)
371 continue;
372 }
373 plen = s - p;
374
375 if (plen == sizeof("pre:") - 1 &&
376 memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
377 mode = S_PRE;
378 else if (plen == sizeof("post:") - 1 &&
379 memcmp(p, "post:", sizeof("post:") - 1) == 0)
380 mode = S_POST;
381 else if (*s != '\0' || (*s == '\0' && !was_space)) {
382 if (mode == S_PRE) {
383 dep->pre[n_pre] = itr;
384 memcpy(itr, p, plen);
385 itr[plen] = '\0';
386 itr += plen + 1;
387 n_pre++;
388 } else if (mode == S_POST) {
389 dep->post[n_post] = itr;
390 memcpy(itr, p, plen);
391 itr[plen] = '\0';
392 itr += plen + 1;
393 n_post++;
394 }
395 }
396 p = s + 1;
397 if (*s == '\0')
398 break;
399 }
400
401 list = kmod_list_append(config->softdeps, dep);
402 if (list == NULL) {
403 free(dep);
404 return -ENOMEM;
405 }
406 config->softdeps = list;
407
408 return 0;
409 }
410
411 static 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
418 static 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
440 static 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
446 fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC);
447 if (fd < 0) {
448 err = -errno;
449 DBG(config->ctx, "could not open '/proc/cmdline' for reading: %m\n");
450 return err;
451 }
452
453 err = read_str_safe(fd, buf, sizeof(buf));
454 close(fd);
455 if (err < 0) {
456 ERR(config->ctx, "could not read from '/proc/cmdline': %s\n",
457 strerror(-err));
458 return err;
459 }
460
461 for (p = buf, modname = buf; *p != '\0' && *p != '\n'; p++) {
462 switch (*p) {
463 case ' ':
464 *p = '\0';
465 kcmdline_parse_result(config, modname, param, value);
466 param = value = NULL;
467 modname = p + 1;
468 break;
469 case '.':
470 *p = '\0';
471 param = p + 1;
472 break;
473 case '=':
474 if (param != NULL)
475 value = p + 1;
476 break;
477 }
478 }
479
480 *p = '\0';
481 kcmdline_parse_result(config, modname, param, value);
482
483 return 0;
484 }
485
486 /*
487 * Take an fd and own it. It will be closed on return. filename is used only
488 * for debug messages
489 */
490 static int kmod_config_parse(struct kmod_config *config, int fd,
491 const char *filename)
492 {
493 struct kmod_ctx *ctx = config->ctx;
494 char *line;
495 FILE *fp;
496 unsigned int linenum = 0;
497 int err;
498
499 fp = fdopen(fd, "r");
500 if (fp == NULL) {
501 err = -errno;
502 ERR(config->ctx, "fd %d: %m", fd);
503 close(fd);
504 return err;
505 }
506
507 while ((line = getline_wrapped(fp, &linenum)) != NULL) {
508 char *cmd, *saveptr;
509
510 if (line[0] == '\0' || line[0] == '#')
511 goto done_next;
512
513 cmd = strtok_r(line, "\t ", &saveptr);
514 if (cmd == NULL)
515 goto done_next;
516
517 if (streq(cmd, "alias")) {
518 char *alias = strtok_r(NULL, "\t ", &saveptr);
519 char *modname = strtok_r(NULL, "\t ", &saveptr);
520
521 if (alias == NULL || modname == NULL)
522 goto syntax_error;
523
524 kmod_config_add_alias(config,
525 underscores(ctx, alias),
526 underscores(ctx, modname));
527 } else if (streq(cmd, "blacklist")) {
528 char *modname = strtok_r(NULL, "\t ", &saveptr);
529
530 if (modname == NULL)
531 goto syntax_error;
532
533 kmod_config_add_blacklist(config,
534 underscores(ctx, modname));
535 } else if (streq(cmd, "options")) {
536 char *modname = strtok_r(NULL, "\t ", &saveptr);
537 char *options = strtok_r(NULL, "\0", &saveptr);
538
539 if (modname == NULL || options == NULL)
540 goto syntax_error;
541
542 kmod_config_add_options(config,
543 underscores(ctx, modname),
544 options);
545 } else if (streq(cmd, "install")) {
546 char *modname = strtok_r(NULL, "\t ", &saveptr);
547 char *installcmd = strtok_r(NULL, "\0", &saveptr);
548
549 if (modname == NULL || installcmd == NULL)
550 goto syntax_error;
551
552 kmod_config_add_command(config,
553 underscores(ctx, modname),
554 installcmd,
555 cmd, &config->install_commands);
556 } else if (streq(cmd, "remove")) {
557 char *modname = strtok_r(NULL, "\t ", &saveptr);
558 char *removecmd = strtok_r(NULL, "\0", &saveptr);
559
560 if (modname == NULL || removecmd == NULL)
561 goto syntax_error;
562
563 kmod_config_add_command(config,
564 underscores(ctx, modname),
565 removecmd,
566 cmd, &config->remove_commands);
567 } else if (streq(cmd, "softdep")) {
568 char *modname = strtok_r(NULL, "\t ", &saveptr);
569 char *softdeps = strtok_r(NULL, "\0", &saveptr);
570
571 if (modname == NULL || softdeps == NULL)
572 goto syntax_error;
573
574 kmod_config_add_softdep(config,
575 underscores(ctx, modname),
576 softdeps);
577 } else if (streq(cmd, "include")
578 || streq(cmd, "config")) {
579 ERR(ctx, "%s: command %s is deprecated and not parsed anymore\n",
580 filename, cmd);
581 } else {
582 syntax_error:
583 ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n",
584 filename, linenum, cmd);
585 }
586
587 done_next:
588 free(line);
589 }
590
591 fclose(fp);
592
593 return 0;
594 }
595
596 void kmod_config_free(struct kmod_config *config)
597 {
598 while (config->aliases)
599 kmod_config_free_alias(config, config->aliases);
600
601 while (config->blacklists)
602 kmod_config_free_blacklist(config, config->blacklists);
603
604 while (config->options)
605 kmod_config_free_options(config, config->options);
606
607 while (config->install_commands) {
608 kmod_config_free_command(config, config->install_commands,
609 &config->install_commands);
610 }
611
612 while (config->remove_commands) {
613 kmod_config_free_command(config, config->remove_commands,
614 &config->remove_commands);
615 }
616
617 while (config->softdeps)
618 kmod_config_free_softdep(config, config->softdeps);
619
620 for (; config->paths != NULL;
621 config->paths = kmod_list_remove(config->paths))
622 free(config->paths->data);
623
624 free(config);
625 }
626
627 static bool conf_files_filter_out(struct kmod_ctx *ctx, DIR *d,
628 const char *path, const char *fn)
629 {
630 size_t len = strlen(fn);
631 struct stat st;
632
633 if (fn[0] == '.')
634 return true;
635
636 if (len < 6 || (!streq(&fn[len - 5], ".conf")
637 && !streq(&fn[len - 6], ".alias")))
638 return true;
639
640 fstatat(dirfd(d), fn, &st, 0);
641
642 if (S_ISDIR(st.st_mode)) {
643 ERR(ctx, "Directories inside directories are not supported: "
644 "%s/%s\n", path, fn);
645 return true;
646 }
647
648 return false;
649 }
650
651 struct conf_file {
652 const char *path;
653 bool is_single;
654 char name[];
655 };
656
657 static int conf_files_insert_sorted(struct kmod_ctx *ctx,
658 struct kmod_list **list,
659 const char *path, const char *name)
660 {
661 struct kmod_list *lpos, *tmp;
662 struct conf_file *cf;
663 size_t namelen;
664 int cmp = -1;
665 bool is_single = false;
666
667 if (name == NULL) {
668 name = basename(path);
669 is_single = true;
670 }
671
672 kmod_list_foreach(lpos, *list) {
673 cf = lpos->data;
674
675 if ((cmp = strcmp(name, cf->name)) <= 0)
676 break;
677 }
678
679 if (cmp == 0) {
680 DBG(ctx, "Ignoring duplicate config file: %s/%s\n", path,
681 name);
682 return -EEXIST;
683 }
684
685 namelen = strlen(name);
686 cf = malloc(sizeof(*cf) + namelen + 1);
687 if (cf == NULL)
688 return -ENOMEM;
689
690 memcpy(cf->name, name, namelen + 1);
691 cf->path = path;
692 cf->is_single = is_single;
693
694 if (lpos == NULL)
695 tmp = kmod_list_append(*list, cf);
696 else if (lpos == *list)
697 tmp = kmod_list_prepend(*list, cf);
698 else
699 tmp = kmod_list_insert_before(lpos, cf);
700
701 if (tmp == NULL) {
702 free(cf);
703 return -ENOMEM;
704 }
705
706 if (lpos == NULL || lpos == *list)
707 *list = tmp;
708
709 return 0;
710 }
711
712 /*
713 * Insert configuration files in @list, ignoring duplicates
714 */
715 static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
716 const char *path,
717 unsigned long long *path_stamp)
718 {
719 DIR *d;
720 int err;
721 struct stat st;
722
723 if (stat(path, &st) != 0) {
724 err = -errno;
725 DBG(ctx, "could not stat '%s': %m\n", path);
726 return err;
727 }
728
729 *path_stamp = ts_usec(&st.st_mtim);
730
731 if (S_ISREG(st.st_mode)) {
732 conf_files_insert_sorted(ctx, list, path, NULL);
733 return 0;
734 } if (!S_ISDIR(st.st_mode)) {
735 ERR(ctx, "unsupported file mode %s: %#x\n",
736 path, st.st_mode);
737 return -EINVAL;
738 }
739
740 d = opendir(path);
741 if (d == NULL) {
742 ERR(ctx, "%m\n");
743 return -EINVAL;
744 }
745
746 for (;;) {
747 struct dirent ent, *entp;
748
749 err = readdir_r(d, &ent, &entp);
750 if (err != 0) {
751 ERR(ctx, "reading entry %s\n", strerror(-err));
752 goto fail_read;
753 }
754
755 if (entp == NULL)
756 break;
757
758 if (conf_files_filter_out(ctx, d, path, entp->d_name))
759 continue;
760
761 conf_files_insert_sorted(ctx, list, path, entp->d_name);
762 }
763
764 closedir(d);
765 return 0;
766
767 fail_read:
768 closedir(d);
769 return err;
770 }
771
772 int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config,
773 const char * const *config_paths)
774 {
775 struct kmod_config *config;
776 struct kmod_list *list = NULL;
777 struct kmod_list *path_list = NULL;
778 size_t i;
779
780 for (i = 0; config_paths[i] != NULL; i++) {
781 const char *path = config_paths[i];
782 unsigned long long path_stamp = 0;
783 size_t pathlen;
784 struct kmod_list *tmp;
785 struct kmod_config_path *cf;
786
787 if (conf_files_list(ctx, &list, path, &path_stamp) < 0)
788 continue;
789
790 pathlen = strlen(path) + 1;
791 cf = malloc(sizeof(*cf) + pathlen);
792 if (cf == NULL)
793 goto oom;
794
795 cf->stamp = path_stamp;
796 memcpy(cf->path, path, pathlen);
797
798 tmp = kmod_list_append(path_list, cf);
799 if (tmp == NULL)
800 goto oom;
801 path_list = tmp;
802 }
803
804 *p_config = config = calloc(1, sizeof(struct kmod_config));
805 if (config == NULL)
806 goto oom;
807
808 config->paths = path_list;
809 config->ctx = ctx;
810
811 for (; list != NULL; list = kmod_list_remove(list)) {
812 char fn[PATH_MAX];
813 struct conf_file *cf = list->data;
814 int fd;
815
816 if (cf->is_single)
817 strcpy(fn, cf->path);
818 else
819 snprintf(fn, sizeof(fn),"%s/%s", cf->path,
820 cf->name);
821
822 fd = open(fn, O_RDONLY|O_CLOEXEC);
823 DBG(ctx, "parsing file '%s' fd=%d\n", fn, fd);
824
825 if (fd >= 0)
826 kmod_config_parse(config, fd, fn);
827
828 free(cf);
829 }
830
831 kmod_config_parse_kcmdline(config);
832
833 return 0;
834
835 oom:
836 for (; list != NULL; list = kmod_list_remove(list))
837 free(list->data);
838
839 for (; path_list != NULL; path_list = kmod_list_remove(path_list))
840 free(path_list->data);
841
842 return -ENOMEM;
843 }