]>
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; | |
aa878540 | 499 | char *p, *modname, *param = NULL, *value = NULL, is_module = 1; |
1684e440 | 500 | |
79e5ea91 | 501 | fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC); |
dd1cf10f LDM |
502 | if (fd < 0) { |
503 | err = -errno; | |
504 | DBG(config->ctx, "could not open '/proc/cmdline' for reading: %m\n"); | |
505 | return err; | |
506 | } | |
507 | ||
1684e440 LDM |
508 | err = read_str_safe(fd, buf, sizeof(buf)); |
509 | close(fd); | |
510 | if (err < 0) { | |
511 | ERR(config->ctx, "could not read from '/proc/cmdline': %s\n", | |
512 | strerror(-err)); | |
513 | return err; | |
514 | } | |
515 | ||
516 | for (p = buf, modname = buf; *p != '\0' && *p != '\n'; p++) { | |
517 | switch (*p) { | |
518 | case ' ': | |
519 | *p = '\0'; | |
aa878540 MM |
520 | if (is_module) |
521 | kcmdline_parse_result(config, modname, param, value); | |
1684e440 LDM |
522 | param = value = NULL; |
523 | modname = p + 1; | |
aa878540 | 524 | is_module = 1; |
1684e440 LDM |
525 | break; |
526 | case '.': | |
66f3228d LDM |
527 | if (param == NULL) { |
528 | *p = '\0'; | |
529 | param = p + 1; | |
530 | } | |
1684e440 LDM |
531 | break; |
532 | case '=': | |
135bffd6 LDM |
533 | if (param != NULL) |
534 | value = p + 1; | |
aa878540 MM |
535 | else |
536 | is_module = 0; | |
1684e440 LDM |
537 | break; |
538 | } | |
539 | } | |
540 | ||
541 | *p = '\0'; | |
aa878540 MM |
542 | if (is_module) |
543 | kcmdline_parse_result(config, modname, param, value); | |
1684e440 LDM |
544 | |
545 | return 0; | |
546 | } | |
547 | ||
b7b7ac29 LDM |
548 | /* |
549 | * Take an fd and own it. It will be closed on return. filename is used only | |
550 | * for debug messages | |
551 | */ | |
552 | static int kmod_config_parse(struct kmod_config *config, int fd, | |
553 | const char *filename) | |
7c2ab358 | 554 | { |
d13e606f | 555 | struct kmod_ctx *ctx = config->ctx; |
7c2ab358 LDM |
556 | char *line; |
557 | FILE *fp; | |
759214fa | 558 | unsigned int linenum = 0; |
b7b7ac29 | 559 | int err; |
7c2ab358 | 560 | |
b7b7ac29 LDM |
561 | fp = fdopen(fd, "r"); |
562 | if (fp == NULL) { | |
563 | err = -errno; | |
050db08c | 564 | ERR(config->ctx, "fd %d: %m\n", fd); |
b7b7ac29 LDM |
565 | close(fd); |
566 | return err; | |
567 | } | |
7c2ab358 | 568 | |
aafd3835 | 569 | while ((line = freadline_wrapped(fp, &linenum)) != NULL) { |
c11e62bf | 570 | char *cmd, *saveptr; |
7c2ab358 LDM |
571 | |
572 | if (line[0] == '\0' || line[0] == '#') | |
573 | goto done_next; | |
574 | ||
c11e62bf | 575 | cmd = strtok_r(line, "\t ", &saveptr); |
7c2ab358 LDM |
576 | if (cmd == NULL) |
577 | goto done_next; | |
578 | ||
877e80cd | 579 | if (streq(cmd, "alias")) { |
c11e62bf LDM |
580 | char *alias = strtok_r(NULL, "\t ", &saveptr); |
581 | char *modname = strtok_r(NULL, "\t ", &saveptr); | |
7c2ab358 | 582 | |
52c9c990 | 583 | if (underscores(alias) < 0 || underscores(modname) < 0) |
7c2ab358 LDM |
584 | goto syntax_error; |
585 | ||
52c9c990 | 586 | kmod_config_add_alias(config, alias, modname); |
877e80cd | 587 | } else if (streq(cmd, "blacklist")) { |
c11e62bf | 588 | char *modname = strtok_r(NULL, "\t ", &saveptr); |
81cf2060 | 589 | |
52c9c990 | 590 | if (underscores(modname) < 0) |
81cf2060 LDM |
591 | goto syntax_error; |
592 | ||
52c9c990 | 593 | kmod_config_add_blacklist(config, modname); |
615c42be LDM |
594 | } else if (streq(cmd, "options")) { |
595 | char *modname = strtok_r(NULL, "\t ", &saveptr); | |
83121fde | 596 | char *options = strtok_r(NULL, "\0", &saveptr); |
615c42be | 597 | |
52c9c990 | 598 | if (underscores(modname) < 0 || options == NULL) |
615c42be LDM |
599 | goto syntax_error; |
600 | ||
52c9c990 | 601 | kmod_config_add_options(config, modname, options); |
40ee8dad | 602 | } else if (streq(cmd, "install")) { |
a5cce6d6 | 603 | char *modname = strtok_r(NULL, "\t ", &saveptr); |
83121fde | 604 | char *installcmd = strtok_r(NULL, "\0", &saveptr); |
a5cce6d6 | 605 | |
52c9c990 | 606 | if (underscores(modname) < 0 || installcmd == NULL) |
a5cce6d6 LDM |
607 | goto syntax_error; |
608 | ||
52c9c990 | 609 | kmod_config_add_command(config, modname, installcmd, |
a5cce6d6 | 610 | cmd, &config->install_commands); |
40ee8dad | 611 | } else if (streq(cmd, "remove")) { |
a5cce6d6 | 612 | char *modname = strtok_r(NULL, "\t ", &saveptr); |
83121fde | 613 | char *removecmd = strtok_r(NULL, "\0", &saveptr); |
a5cce6d6 | 614 | |
52c9c990 | 615 | if (underscores(modname) < 0 || removecmd == NULL) |
a5cce6d6 LDM |
616 | goto syntax_error; |
617 | ||
52c9c990 | 618 | kmod_config_add_command(config, modname, removecmd, |
a5cce6d6 | 619 | cmd, &config->remove_commands); |
40ee8dad | 620 | } else if (streq(cmd, "softdep")) { |
1c522600 | 621 | char *modname = strtok_r(NULL, "\t ", &saveptr); |
83121fde | 622 | char *softdeps = strtok_r(NULL, "\0", &saveptr); |
1c522600 | 623 | |
52c9c990 | 624 | if (underscores(modname) < 0 || softdeps == NULL) |
1c522600 GSB |
625 | goto syntax_error; |
626 | ||
52c9c990 | 627 | kmod_config_add_softdep(config, modname, softdeps); |
615c42be | 628 | } else if (streq(cmd, "include") |
877e80cd | 629 | || streq(cmd, "config")) { |
0ad5dd08 | 630 | ERR(ctx, "%s: command %s is deprecated and not parsed anymore\n", |
81cf2060 | 631 | filename, cmd); |
7c2ab358 LDM |
632 | } else { |
633 | syntax_error: | |
634 | ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n", | |
635 | filename, linenum, cmd); | |
636 | } | |
637 | ||
638 | done_next: | |
639 | free(line); | |
640 | } | |
641 | ||
642 | fclose(fp); | |
643 | ||
644 | return 0; | |
645 | } | |
646 | ||
d13e606f | 647 | void kmod_config_free(struct kmod_config *config) |
7c2ab358 LDM |
648 | { |
649 | while (config->aliases) | |
d13e606f | 650 | kmod_config_free_alias(config, config->aliases); |
81cf2060 LDM |
651 | |
652 | while (config->blacklists) | |
d13e606f GSB |
653 | kmod_config_free_blacklist(config, config->blacklists); |
654 | ||
615c42be LDM |
655 | while (config->options) |
656 | kmod_config_free_options(config, config->options); | |
657 | ||
a5cce6d6 LDM |
658 | while (config->install_commands) { |
659 | kmod_config_free_command(config, config->install_commands, | |
660 | &config->install_commands); | |
661 | } | |
662 | ||
663 | while (config->remove_commands) { | |
664 | kmod_config_free_command(config, config->remove_commands, | |
665 | &config->remove_commands); | |
666 | } | |
667 | ||
1c522600 GSB |
668 | while (config->softdeps) |
669 | kmod_config_free_softdep(config, config->softdeps); | |
670 | ||
b6a4dfb1 LDM |
671 | for (; config->paths != NULL; |
672 | config->paths = kmod_list_remove(config->paths)) | |
673 | free(config->paths->data); | |
674 | ||
d13e606f | 675 | free(config); |
7c2ab358 LDM |
676 | } |
677 | ||
98c80f44 LDM |
678 | static bool conf_files_filter_out(struct kmod_ctx *ctx, DIR *d, |
679 | const char *path, const char *fn) | |
7c2ab358 LDM |
680 | { |
681 | size_t len = strlen(fn); | |
98c80f44 | 682 | struct stat st; |
7c2ab358 LDM |
683 | |
684 | if (fn[0] == '.') | |
8f767e2d | 685 | return true; |
7c2ab358 | 686 | |
877e80cd | 687 | if (len < 6 || (!streq(&fn[len - 5], ".conf") |
9070b117 | 688 | && !streq(&fn[len - 6], ".alias"))) |
8f767e2d | 689 | return true; |
7c2ab358 | 690 | |
98c80f44 LDM |
691 | fstatat(dirfd(d), fn, &st, 0); |
692 | ||
693 | if (S_ISDIR(st.st_mode)) { | |
694 | ERR(ctx, "Directories inside directories are not supported: " | |
695 | "%s/%s\n", path, fn); | |
8f767e2d | 696 | return true; |
98c80f44 LDM |
697 | } |
698 | ||
8f767e2d | 699 | return false; |
7c2ab358 LDM |
700 | } |
701 | ||
7fe5f7ab LDM |
702 | struct conf_file { |
703 | const char *path; | |
704 | bool is_single; | |
705 | char name[]; | |
706 | }; | |
707 | ||
708 | static int conf_files_insert_sorted(struct kmod_ctx *ctx, | |
709 | struct kmod_list **list, | |
710 | const char *path, const char *name) | |
711 | { | |
712 | struct kmod_list *lpos, *tmp; | |
713 | struct conf_file *cf; | |
714 | size_t namelen; | |
715 | int cmp = -1; | |
716 | bool is_single = false; | |
717 | ||
718 | if (name == NULL) { | |
719 | name = basename(path); | |
720 | is_single = true; | |
721 | } | |
722 | ||
723 | kmod_list_foreach(lpos, *list) { | |
724 | cf = lpos->data; | |
725 | ||
726 | if ((cmp = strcmp(name, cf->name)) <= 0) | |
727 | break; | |
728 | } | |
729 | ||
730 | if (cmp == 0) { | |
731 | DBG(ctx, "Ignoring duplicate config file: %s/%s\n", path, | |
732 | name); | |
733 | return -EEXIST; | |
734 | } | |
735 | ||
736 | namelen = strlen(name); | |
737 | cf = malloc(sizeof(*cf) + namelen + 1); | |
738 | if (cf == NULL) | |
739 | return -ENOMEM; | |
740 | ||
741 | memcpy(cf->name, name, namelen + 1); | |
742 | cf->path = path; | |
743 | cf->is_single = is_single; | |
744 | ||
745 | if (lpos == NULL) | |
746 | tmp = kmod_list_append(*list, cf); | |
747 | else if (lpos == *list) | |
748 | tmp = kmod_list_prepend(*list, cf); | |
749 | else | |
750 | tmp = kmod_list_insert_before(lpos, cf); | |
751 | ||
752 | if (tmp == NULL) { | |
753 | free(cf); | |
754 | return -ENOMEM; | |
755 | } | |
756 | ||
757 | if (lpos == NULL || lpos == *list) | |
758 | *list = tmp; | |
759 | ||
760 | return 0; | |
761 | } | |
762 | ||
4782396c | 763 | /* |
7fe5f7ab | 764 | * Insert configuration files in @list, ignoring duplicates |
4782396c | 765 | */ |
7fe5f7ab | 766 | static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list, |
b6a4dfb1 LDM |
767 | const char *path, |
768 | unsigned long long *path_stamp) | |
7c2ab358 | 769 | { |
7c2ab358 LDM |
770 | DIR *d; |
771 | int err; | |
7fe5f7ab | 772 | struct stat st; |
7e0385c4 | 773 | struct dirent *dent; |
7c2ab358 | 774 | |
7fe5f7ab LDM |
775 | if (stat(path, &st) != 0) { |
776 | err = -errno; | |
777 | DBG(ctx, "could not stat '%s': %m\n", path); | |
778 | return err; | |
779 | } | |
780 | ||
6068aaae | 781 | *path_stamp = stat_mstamp(&st); |
b6a4dfb1 | 782 | |
519d27de | 783 | if (!S_ISDIR(st.st_mode)) { |
7fe5f7ab LDM |
784 | conf_files_insert_sorted(ctx, list, path, NULL); |
785 | return 0; | |
7fe5f7ab | 786 | } |
1c250ec1 | 787 | |
7c2ab358 LDM |
788 | d = opendir(path); |
789 | if (d == NULL) { | |
dfa96f15 | 790 | ERR(ctx, "opendir(%s): %m\n", path); |
7fe5f7ab | 791 | return -EINVAL; |
7c2ab358 LDM |
792 | } |
793 | ||
7e0385c4 LDM |
794 | for (dent = readdir(d); dent != NULL; dent = readdir(d)) { |
795 | if (conf_files_filter_out(ctx, d, path, dent->d_name)) | |
7c2ab358 LDM |
796 | continue; |
797 | ||
7e0385c4 | 798 | conf_files_insert_sorted(ctx, list, path, dent->d_name); |
b7b7ac29 | 799 | } |
7c2ab358 | 800 | |
7fe5f7ab LDM |
801 | closedir(d); |
802 | return 0; | |
7c2ab358 LDM |
803 | } |
804 | ||
c35347f1 LDM |
805 | int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config, |
806 | const char * const *config_paths) | |
7c2ab358 | 807 | { |
d13e606f | 808 | struct kmod_config *config; |
7fe5f7ab | 809 | struct kmod_list *list = NULL; |
b6a4dfb1 | 810 | struct kmod_list *path_list = NULL; |
b7b7ac29 | 811 | size_t i; |
7c2ab358 | 812 | |
8240333b TG |
813 | conf_files_insert_sorted(ctx, &list, kmod_get_dirname(ctx), "modules.softdep"); |
814 | ||
cb8d4d3e GSB |
815 | for (i = 0; config_paths[i] != NULL; i++) { |
816 | const char *path = config_paths[i]; | |
b6a4dfb1 LDM |
817 | unsigned long long path_stamp = 0; |
818 | size_t pathlen; | |
819 | struct kmod_list *tmp; | |
820 | struct kmod_config_path *cf; | |
821 | ||
822 | if (conf_files_list(ctx, &list, path, &path_stamp) < 0) | |
823 | continue; | |
824 | ||
825 | pathlen = strlen(path) + 1; | |
826 | cf = malloc(sizeof(*cf) + pathlen); | |
827 | if (cf == NULL) | |
828 | goto oom; | |
7c2ab358 | 829 | |
b6a4dfb1 LDM |
830 | cf->stamp = path_stamp; |
831 | memcpy(cf->path, path, pathlen); | |
832 | ||
833 | tmp = kmod_list_append(path_list, cf); | |
834 | if (tmp == NULL) | |
835 | goto oom; | |
836 | path_list = tmp; | |
7fe5f7ab | 837 | } |
cb8d4d3e | 838 | |
b6a4dfb1 LDM |
839 | *p_config = config = calloc(1, sizeof(struct kmod_config)); |
840 | if (config == NULL) | |
841 | goto oom; | |
842 | ||
843 | config->paths = path_list; | |
29b69c0b | 844 | config->ctx = ctx; |
b6a4dfb1 | 845 | |
7fe5f7ab LDM |
846 | for (; list != NULL; list = kmod_list_remove(list)) { |
847 | char fn[PATH_MAX]; | |
848 | struct conf_file *cf = list->data; | |
849 | int fd; | |
cb8d4d3e | 850 | |
7fe5f7ab LDM |
851 | if (cf->is_single) |
852 | strcpy(fn, cf->path); | |
853 | else | |
854 | snprintf(fn, sizeof(fn),"%s/%s", cf->path, | |
855 | cf->name); | |
7c2ab358 | 856 | |
7fe5f7ab LDM |
857 | fd = open(fn, O_RDONLY|O_CLOEXEC); |
858 | DBG(ctx, "parsing file '%s' fd=%d\n", fn, fd); | |
7c2ab358 | 859 | |
7fe5f7ab LDM |
860 | if (fd >= 0) |
861 | kmod_config_parse(config, fd, fn); | |
7c2ab358 | 862 | |
7fe5f7ab | 863 | free(cf); |
7c2ab358 LDM |
864 | } |
865 | ||
1684e440 LDM |
866 | kmod_config_parse_kcmdline(config); |
867 | ||
b7b7ac29 | 868 | return 0; |
b6a4dfb1 LDM |
869 | |
870 | oom: | |
871 | for (; list != NULL; list = kmod_list_remove(list)) | |
872 | free(list->data); | |
873 | ||
874 | for (; path_list != NULL; path_list = kmod_list_remove(path_list)) | |
875 | free(path_list->data); | |
876 | ||
877 | return -ENOMEM; | |
7c2ab358 | 878 | } |
00178629 LDM |
879 | |
880 | /********************************************************************** | |
881 | * struct kmod_config_iter functions | |
882 | **********************************************************************/ | |
883 | ||
884 | enum config_type { | |
885 | CONFIG_TYPE_BLACKLIST = 0, | |
886 | CONFIG_TYPE_INSTALL, | |
887 | CONFIG_TYPE_REMOVE, | |
888 | CONFIG_TYPE_ALIAS, | |
889 | CONFIG_TYPE_OPTION, | |
890 | CONFIG_TYPE_SOFTDEP, | |
891 | }; | |
892 | ||
893 | struct kmod_config_iter { | |
894 | enum config_type type; | |
6b04ef32 | 895 | bool intermediate; |
00178629 LDM |
896 | const struct kmod_list *list; |
897 | const struct kmod_list *curr; | |
6b04ef32 | 898 | void *data; |
00178629 LDM |
899 | const char *(*get_key)(const struct kmod_list *l); |
900 | const char *(*get_value)(const struct kmod_list *l); | |
901 | }; | |
902 | ||
6b04ef32 LDM |
903 | static const char *softdep_get_plain_softdep(const struct kmod_list *l) |
904 | { | |
905 | char *s = softdep_to_char(l->data); | |
906 | return s; | |
907 | } | |
908 | ||
00178629 LDM |
909 | static struct kmod_config_iter *kmod_config_iter_new(const struct kmod_ctx* ctx, |
910 | enum config_type type) | |
911 | { | |
912 | struct kmod_config_iter *iter = calloc(1, sizeof(*iter)); | |
e7fc2c86 | 913 | const struct kmod_config *config = kmod_get_config(ctx); |
00178629 LDM |
914 | |
915 | if (iter == NULL) | |
916 | return NULL; | |
917 | ||
918 | iter->type = type; | |
919 | ||
920 | switch (type) { | |
921 | case CONFIG_TYPE_BLACKLIST: | |
e7fc2c86 | 922 | iter->list = config->blacklists; |
00178629 LDM |
923 | iter->get_key = kmod_blacklist_get_modname; |
924 | break; | |
925 | case CONFIG_TYPE_INSTALL: | |
e7fc2c86 | 926 | iter->list = config->install_commands; |
00178629 LDM |
927 | iter->get_key = kmod_command_get_modname; |
928 | iter->get_value = kmod_command_get_command; | |
929 | break; | |
930 | case CONFIG_TYPE_REMOVE: | |
e7fc2c86 | 931 | iter->list = config->remove_commands; |
00178629 LDM |
932 | iter->get_key = kmod_command_get_modname; |
933 | iter->get_value = kmod_command_get_command; | |
934 | break; | |
935 | case CONFIG_TYPE_ALIAS: | |
e7fc2c86 | 936 | iter->list = config->aliases; |
00178629 LDM |
937 | iter->get_key = kmod_alias_get_name; |
938 | iter->get_value = kmod_alias_get_modname; | |
939 | break; | |
940 | case CONFIG_TYPE_OPTION: | |
e7fc2c86 | 941 | iter->list = config->options; |
00178629 LDM |
942 | iter->get_key = kmod_option_get_modname; |
943 | iter->get_value = kmod_option_get_options; | |
944 | break; | |
945 | case CONFIG_TYPE_SOFTDEP: | |
e7fc2c86 | 946 | iter->list = config->softdeps; |
00178629 | 947 | iter->get_key = kmod_softdep_get_name; |
6b04ef32 LDM |
948 | iter->get_value = softdep_get_plain_softdep; |
949 | iter->intermediate = true; | |
00178629 LDM |
950 | break; |
951 | } | |
952 | ||
953 | return iter; | |
954 | } | |
955 | ||
2f47c7fa LDM |
956 | /** |
957 | * SECTION:libkmod-config | |
958 | * @short_description: retrieve current libkmod configuration | |
959 | */ | |
960 | ||
961 | /** | |
962 | * kmod_config_get_blacklists: | |
963 | * @ctx: kmod library context | |
964 | * | |
965 | * Retrieve an iterator to deal with the blacklist maintained inside the | |
966 | * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and | |
967 | * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must | |
968 | * be made to initialize the iterator and check if it's valid. | |
969 | * | |
883d8c42 | 970 | * Returns: a new iterator over the blacklists or NULL on failure. Free it |
2f47c7fa LDM |
971 | * with kmod_config_iter_free_iter(). |
972 | */ | |
00178629 LDM |
973 | KMOD_EXPORT struct kmod_config_iter *kmod_config_get_blacklists(const struct kmod_ctx *ctx) |
974 | { | |
975 | if (ctx == NULL) | |
976 | return NULL;; | |
977 | ||
978 | return kmod_config_iter_new(ctx, CONFIG_TYPE_BLACKLIST); | |
979 | } | |
980 | ||
2f47c7fa LDM |
981 | /** |
982 | * kmod_config_get_install_commands: | |
983 | * @ctx: kmod library context | |
984 | * | |
985 | * Retrieve an iterator to deal with the install commands maintained inside the | |
986 | * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and | |
987 | * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must | |
988 | * be made to initialize the iterator and check if it's valid. | |
989 | * | |
883d8c42 | 990 | * Returns: a new iterator over the install commands or NULL on failure. Free |
2f47c7fa LDM |
991 | * it with kmod_config_iter_free_iter(). |
992 | */ | |
00178629 LDM |
993 | KMOD_EXPORT struct kmod_config_iter *kmod_config_get_install_commands(const struct kmod_ctx *ctx) |
994 | { | |
995 | if (ctx == NULL) | |
996 | return NULL;; | |
997 | ||
998 | return kmod_config_iter_new(ctx, CONFIG_TYPE_INSTALL); | |
999 | } | |
1000 | ||
2f47c7fa LDM |
1001 | /** |
1002 | * kmod_config_get_remove_commands: | |
1003 | * @ctx: kmod library context | |
1004 | * | |
1005 | * Retrieve an iterator to deal with the remove commands maintained inside the | |
1006 | * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and | |
1007 | * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must | |
1008 | * be made to initialize the iterator and check if it's valid. | |
1009 | * | |
883d8c42 | 1010 | * Returns: a new iterator over the remove commands or NULL on failure. Free |
2f47c7fa LDM |
1011 | * it with kmod_config_iter_free_iter(). |
1012 | */ | |
00178629 LDM |
1013 | KMOD_EXPORT struct kmod_config_iter *kmod_config_get_remove_commands(const struct kmod_ctx *ctx) |
1014 | { | |
1015 | if (ctx == NULL) | |
1016 | return NULL;; | |
1017 | ||
1018 | return kmod_config_iter_new(ctx, CONFIG_TYPE_REMOVE); | |
1019 | } | |
1020 | ||
2f47c7fa LDM |
1021 | /** |
1022 | * kmod_config_get_aliases: | |
1023 | * @ctx: kmod library context | |
1024 | * | |
1025 | * Retrieve an iterator to deal with the aliases maintained inside the | |
1026 | * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and | |
1027 | * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must | |
1028 | * be made to initialize the iterator and check if it's valid. | |
1029 | * | |
883d8c42 | 1030 | * Returns: a new iterator over the aliases or NULL on failure. Free it with |
2f47c7fa LDM |
1031 | * kmod_config_iter_free_iter(). |
1032 | */ | |
00178629 LDM |
1033 | KMOD_EXPORT struct kmod_config_iter *kmod_config_get_aliases(const struct kmod_ctx *ctx) |
1034 | { | |
1035 | if (ctx == NULL) | |
1036 | return NULL;; | |
1037 | ||
1038 | return kmod_config_iter_new(ctx, CONFIG_TYPE_ALIAS); | |
1039 | } | |
1040 | ||
2f47c7fa LDM |
1041 | /** |
1042 | * kmod_config_get_options: | |
1043 | * @ctx: kmod library context | |
1044 | * | |
1045 | * Retrieve an iterator to deal with the options maintained inside the | |
1046 | * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and | |
1047 | * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must | |
1048 | * be made to initialize the iterator and check if it's valid. | |
1049 | * | |
883d8c42 | 1050 | * Returns: a new iterator over the options or NULL on failure. Free it with |
2f47c7fa LDM |
1051 | * kmod_config_iter_free_iter(). |
1052 | */ | |
00178629 LDM |
1053 | KMOD_EXPORT struct kmod_config_iter *kmod_config_get_options(const struct kmod_ctx *ctx) |
1054 | { | |
1055 | if (ctx == NULL) | |
1056 | return NULL;; | |
1057 | ||
1058 | return kmod_config_iter_new(ctx, CONFIG_TYPE_OPTION); | |
1059 | } | |
1060 | ||
2f47c7fa LDM |
1061 | /** |
1062 | * kmod_config_get_softdeps: | |
1063 | * @ctx: kmod library context | |
1064 | * | |
1065 | * Retrieve an iterator to deal with the softdeps maintained inside the | |
1066 | * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and | |
1067 | * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must | |
1068 | * be made to initialize the iterator and check if it's valid. | |
1069 | * | |
883d8c42 | 1070 | * Returns: a new iterator over the softdeps or NULL on failure. Free it with |
2f47c7fa LDM |
1071 | * kmod_config_iter_free_iter(). |
1072 | */ | |
00178629 LDM |
1073 | KMOD_EXPORT struct kmod_config_iter *kmod_config_get_softdeps(const struct kmod_ctx *ctx) |
1074 | { | |
1075 | if (ctx == NULL) | |
1076 | return NULL;; | |
1077 | ||
1078 | return kmod_config_iter_new(ctx, CONFIG_TYPE_SOFTDEP); | |
1079 | } | |
1080 | ||
2f47c7fa LDM |
1081 | /** |
1082 | * kmod_config_iter_get_key: | |
1083 | * @iter: iterator over a certain configuration | |
1084 | * | |
1085 | * When using a new allocated iterator, user must perform a call to | |
1086 | * kmod_config_iter_next() to initialize iterator's position and check if it's | |
1087 | * valid. | |
1088 | * | |
1089 | * Returns: the key of the current configuration pointed by @iter. | |
1090 | */ | |
00178629 LDM |
1091 | KMOD_EXPORT const char *kmod_config_iter_get_key(const struct kmod_config_iter *iter) |
1092 | { | |
1093 | if (iter == NULL || iter->curr == NULL) | |
1094 | return NULL; | |
1095 | ||
1096 | return iter->get_key(iter->curr); | |
1097 | } | |
1098 | ||
2f47c7fa LDM |
1099 | /** |
1100 | * kmod_config_iter_get_value: | |
1101 | * @iter: iterator over a certain configuration | |
1102 | * | |
1103 | * When using a new allocated iterator, user must perform a call to | |
1104 | * kmod_config_iter_next() to initialize iterator's position and check if it's | |
1105 | * valid. | |
1106 | * | |
1107 | * Returns: the value of the current configuration pointed by @iter. | |
1108 | */ | |
00178629 LDM |
1109 | KMOD_EXPORT const char *kmod_config_iter_get_value(const struct kmod_config_iter *iter) |
1110 | { | |
6b04ef32 LDM |
1111 | const char *s; |
1112 | ||
00178629 LDM |
1113 | if (iter == NULL || iter->curr == NULL) |
1114 | return NULL; | |
1115 | ||
1116 | if (iter->get_value == NULL) | |
1117 | return NULL; | |
1118 | ||
6b04ef32 LDM |
1119 | if (iter->intermediate) { |
1120 | struct kmod_config_iter *i = (struct kmod_config_iter *)iter; | |
1121 | ||
1122 | free(i->data); | |
1123 | s = i->data = (void *) iter->get_value(iter->curr); | |
1124 | } else | |
1125 | s = iter->get_value(iter->curr); | |
1126 | ||
1127 | return s; | |
00178629 LDM |
1128 | } |
1129 | ||
2f47c7fa LDM |
1130 | /** |
1131 | * kmod_config_iter_next: | |
1132 | * @iter: iterator over a certain configuration | |
1133 | * | |
1134 | * Make @iter point to the next item of a certain configuration. It's an | |
1135 | * automatically recycling iterator. When it reaches the end, false is | |
1136 | * returned; then if user wants to iterate again, it's sufficient to call this | |
1137 | * function once more. | |
1138 | * | |
1139 | * Returns: true if next position of @iter is valid or false if its end is | |
1140 | * reached. | |
1141 | */ | |
00178629 LDM |
1142 | KMOD_EXPORT bool kmod_config_iter_next(struct kmod_config_iter *iter) |
1143 | { | |
1144 | if (iter == NULL) | |
1145 | return false; | |
1146 | ||
1147 | if (iter->curr == NULL) { | |
1148 | iter->curr = iter->list; | |
1149 | return iter->curr != NULL; | |
1150 | } | |
1151 | ||
1152 | iter->curr = kmod_list_next(iter->list, iter->curr); | |
1153 | ||
1154 | return iter->curr != NULL; | |
1155 | } | |
1156 | ||
2f47c7fa LDM |
1157 | /** |
1158 | * kmod_config_iter_free_iter: | |
1159 | * @iter: iterator over a certain configuration | |
1160 | * | |
1161 | * Free resources used by the iterator. | |
1162 | */ | |
00178629 LDM |
1163 | KMOD_EXPORT void kmod_config_iter_free_iter(struct kmod_config_iter *iter) |
1164 | { | |
6b04ef32 | 1165 | free(iter->data); |
00178629 LDM |
1166 | free(iter); |
1167 | } |