]>
git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-config.c
2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011 ProFUSION embedded systems
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 version 2.1.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
33 #include "libkmod-private.h"
35 static const char *config_files
[] = {
38 "/usr/local/lib/modprobe",
47 const char *kmod_alias_get_name(const struct kmod_list
*l
) {
48 struct kmod_alias
*alias
= l
->data
;
52 const char *kmod_alias_get_modname(const struct kmod_list
*l
) {
53 struct kmod_alias
*alias
= l
->data
;
54 return alias
->modname
;
57 static struct kmod_list
*add_alias(struct kmod_ctx
*ctx
,
58 struct kmod_list
*aliases
,
59 const char *name
, const char *modname
)
61 struct kmod_alias
*alias
;
63 DBG(ctx
, "name=%s modname=%s\n", name
, modname
);
65 alias
= malloc(sizeof(*alias
));
66 alias
->name
= strdup(name
);
67 alias
->modname
= strdup(modname
);
69 return kmod_list_append(aliases
, alias
);
72 static struct kmod_list
*free_alias(struct kmod_ctx
*ctx
, struct kmod_list
*l
)
74 struct kmod_alias
*alias
= l
->data
;
80 return kmod_list_remove(l
);
83 static struct kmod_list
*add_blacklist(struct kmod_ctx
*ctx
,
84 struct kmod_list
*blacklist
,
89 DBG(ctx
, "modname=%s\n", modname
);
93 return kmod_list_append(blacklist
, p
);
96 static struct kmod_list
*free_blacklist(struct kmod_ctx
*ctx
,
100 return kmod_list_remove(l
);
104 int kmod_parse_config_file(struct kmod_ctx
*ctx
, const char *filename
,
105 struct kmod_config
*config
)
109 unsigned int linenum
;
111 DBG(ctx
, "%s\n", filename
);
113 fp
= fopen(filename
, "r");
117 while ((line
= getline_wrapped(fp
, &linenum
)) != NULL
) {
120 if (line
[0] == '\0' || line
[0] == '#')
123 cmd
= strtok_r(line
, "\t ", &saveptr
);
127 if (!strcmp(cmd
, "alias")) {
128 char *alias
= strtok_r(NULL
, "\t ", &saveptr
);
129 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
131 if (alias
== NULL
|| modname
== NULL
)
134 config
->aliases
= add_alias(ctx
, config
->aliases
,
135 underscores(ctx
, alias
),
136 underscores(ctx
, modname
));
137 } else if (!strcmp(cmd
, "blacklist")) {
138 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
143 config
->blacklists
= add_blacklist(ctx
,
145 underscores(ctx
, modname
));
146 } else if (!strcmp(cmd
, "include") || !strcmp(cmd
, "options")
147 || !strcmp(cmd
, "install")
148 || !strcmp(cmd
, "remove")
149 || !strcmp(cmd
, "softdep")
150 || !strcmp(cmd
, "config")) {
151 INFO(ctx
, "%s: command %s not implemented yet\n",
155 ERR(ctx
, "%s line %u: ignoring bad line starting with '%s'\n",
156 filename
, linenum
, cmd
);
168 void kmod_free_config(struct kmod_ctx
*ctx
, struct kmod_config
*config
)
170 while (config
->aliases
)
171 config
->aliases
= free_alias(ctx
, config
->aliases
);
173 while (config
->blacklists
)
174 config
->blacklists
= free_blacklist(ctx
, config
->blacklists
);
177 static bool conf_files_filter(struct kmod_ctx
*ctx
, const char *path
,
180 size_t len
= strlen(fn
);
185 if (len
< 6 || (strcmp(&fn
[len
- 5], ".conf") != 0
186 && strcmp(&fn
[len
- 6], ".alias"))) {
187 INFO(ctx
, "All config files need .conf: %s/%s, "
188 "it will be ignored in a future release\n",
196 static int conf_files_list(struct kmod_ctx
*ctx
, struct kmod_list
**list
,
197 const char *path
, size_t *n
)
203 if (stat(path
, &st
) < 0)
206 if (!S_ISDIR(st
.st_mode
)) {
207 *list
= kmod_list_append(*list
, (void *)path
);
220 struct dirent ent
, *entp
;
223 err
= readdir_r(d
, &ent
, &entp
);
232 if (conf_files_filter(ctx
, path
, entp
->d_name
) == 1)
235 if (asprintf(&p
, "%s/%s", path
, entp
->d_name
) < 0) {
242 *list
= kmod_list_append(*list
, p
);
251 static int base_cmp(const void *a
, const void *b
)
255 s1
= *(char * const *)a
;
256 s2
= *(char * const *)b
;
258 return strcmp(basename(s1
), basename(s2
));
261 int kmod_parse_config(struct kmod_ctx
*ctx
, struct kmod_config
*config
)
267 struct kmod_list
*list
= NULL
, *l
;
269 for (i
= 0; i
< ARRAY_SIZE(config_files
); i
++)
270 conf_files_list(ctx
, &list
, config_files
[i
], &n
);
272 files
= malloc(sizeof(char *) * n
);
279 kmod_list_foreach(l
, list
) {
284 qsort(files
, n
, sizeof(char *), base_cmp
);
286 for (i
= 0; i
< n
; i
++)
287 kmod_parse_config_file(ctx
, files
[i
], config
);
294 list
= kmod_list_remove(list
);