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