]>
git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-config.c
66d837248a36c87697ccebf93cd591e961fa2246
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
[] = {
46 const char *kmod_alias_get_name(const struct kmod_list
*l
) {
47 struct kmod_alias
*alias
= l
->data
;
51 const char *kmod_alias_get_modname(const struct kmod_list
*l
) {
52 struct kmod_alias
*alias
= l
->data
;
53 return alias
->modname
;
56 static struct kmod_list
*add_alias(struct kmod_ctx
*ctx
,
57 struct kmod_list
*aliases
,
58 const char *name
, const char *modname
)
60 struct kmod_alias
*alias
;
62 DBG(ctx
, "name=%s modname=%s\n", name
, modname
);
64 alias
= malloc(sizeof(*alias
));
65 alias
->name
= strdup(name
);
66 alias
->modname
= strdup(modname
);
68 return kmod_list_append(aliases
, alias
);
71 static struct kmod_list
*free_alias(struct kmod_ctx
*ctx
, struct kmod_list
*l
)
73 struct kmod_alias
*alias
= l
->data
;
79 return kmod_list_remove(l
);
82 static struct kmod_list
*add_blacklist(struct kmod_ctx
*ctx
,
83 struct kmod_list
*blacklist
,
88 DBG(ctx
, "modname=%s\n", modname
);
92 return kmod_list_append(blacklist
, p
);
95 static struct kmod_list
*free_blacklist(struct kmod_ctx
*ctx
,
99 return kmod_list_remove(l
);
103 int kmod_parse_config_file(struct kmod_ctx
*ctx
, const char *filename
,
104 struct kmod_config
*config
)
108 unsigned int linenum
;
110 DBG(ctx
, "%s\n", filename
);
112 fp
= fopen(filename
, "r");
116 while ((line
= getline_wrapped(fp
, &linenum
)) != NULL
) {
119 if (line
[0] == '\0' || line
[0] == '#')
122 cmd
= strtok_r(line
, "\t ", &saveptr
);
126 if (!strcmp(cmd
, "alias")) {
127 char *alias
= strtok_r(NULL
, "\t ", &saveptr
);
128 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
130 if (alias
== NULL
|| modname
== NULL
)
133 config
->aliases
= add_alias(ctx
, config
->aliases
,
134 underscores(ctx
, alias
),
135 underscores(ctx
, modname
));
136 } else if (!strcmp(cmd
, "blacklist")) {
137 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
142 config
->blacklists
= add_blacklist(ctx
,
144 underscores(ctx
, modname
));
145 } else if (!strcmp(cmd
, "include") || !strcmp(cmd
, "options")
146 || !strcmp(cmd
, "install")
147 || !strcmp(cmd
, "remove")
148 || !strcmp(cmd
, "softdep")
149 || !strcmp(cmd
, "config")) {
150 INFO(ctx
, "%s: command %s not implemented yet\n",
154 ERR(ctx
, "%s line %u: ignoring bad line starting with '%s'\n",
155 filename
, linenum
, cmd
);
167 void kmod_free_config(struct kmod_ctx
*ctx
, struct kmod_config
*config
)
169 while (config
->aliases
)
170 config
->aliases
= free_alias(ctx
, config
->aliases
);
172 while (config
->blacklists
)
173 config
->blacklists
= free_blacklist(ctx
, config
->blacklists
);
176 static bool conf_files_filter(struct kmod_ctx
*ctx
, const char *path
,
179 size_t len
= strlen(fn
);
184 if (len
< 6 || (strcmp(&fn
[len
- 5], ".conf") != 0
185 && strcmp(&fn
[len
- 6], ".alias"))) {
186 INFO(ctx
, "All config files need .conf: %s/%s, "
187 "it will be ignored in a future release\n",
195 static int conf_files_list(struct kmod_ctx
*ctx
, struct kmod_list
**list
,
196 const char *path
, size_t *n
)
202 if (stat(path
, &st
) < 0)
205 if (!S_ISDIR(st
.st_mode
)) {
206 *list
= kmod_list_append(*list
, (void *)path
);
219 struct dirent ent
, *entp
;
222 err
= readdir_r(d
, &ent
, &entp
);
231 if (conf_files_filter(ctx
, path
, entp
->d_name
) == 1)
234 if (asprintf(&p
, "%s/%s", path
, entp
->d_name
) < 0) {
241 *list
= kmod_list_append(*list
, p
);
250 static int base_cmp(const void *a
, const void *b
)
254 s1
= *(char * const *)a
;
255 s2
= *(char * const *)b
;
257 return strcmp(basename(s1
), basename(s2
));
260 int kmod_parse_config(struct kmod_ctx
*ctx
, struct kmod_config
*config
)
266 struct kmod_list
*list
= NULL
, *l
;
268 for (i
= 0; i
< ARRAY_SIZE(config_files
); i
++)
269 conf_files_list(ctx
, &list
, config_files
[i
], &n
);
271 files
= malloc(sizeof(char *) * n
);
278 kmod_list_foreach(l
, list
) {
283 qsort(files
, n
, sizeof(char *), base_cmp
);
285 for (i
= 0; i
< n
; i
++)
286 kmod_parse_config_file(ctx
, files
[i
], config
);
293 list
= kmod_list_remove(list
);