blob: 18f300ac338ab8abe6abc1eab5f7189771bb7975 [file] [log] [blame]
Lucas De Marchi7c2ab352011-11-29 18:07:43 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Lucas De Marchi342e9ce2013-11-14 00:33:28 -02005 * Copyright (C) 2013 Intel Corporation. All rights reserved.
Lucas De Marchi7c2ab352011-11-29 18:07:43 -02006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02009 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchi7c2ab352011-11-29 18:07:43 -020011 *
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
Lucas De Marchidea2dfe2014-12-25 23:32:03 -020018 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Lucas De Marchi7c2ab352011-11-29 18:07:43 -020019 */
20
Lucas De Marchic2e42862014-10-03 01:41:42 -030021#include <ctype.h>
22#include <dirent.h>
23#include <errno.h>
24#include <stdarg.h>
25#include <stddef.h>
Lucas De Marchi7c2ab352011-11-29 18:07:43 -020026#include <stdio.h>
27#include <stdlib.h>
Lucas De Marchi7c2ab352011-11-29 18:07:43 -020028#include <string.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030029#include <unistd.h>
Lucas De Marchi7c2ab352011-11-29 18:07:43 -020030#include <sys/stat.h>
31#include <sys/types.h>
Lucas De Marchi7c2ab352011-11-29 18:07:43 -020032
Lucas De Marchi96573a02014-10-03 00:01:35 -030033#include <shared/util.h>
34
Lucas De Marchi7c2ab352011-11-29 18:07:43 -020035#include "libkmod.h"
Lucas De Marchi83b855a2013-07-04 16:13:11 -030036#include "libkmod-internal.h"
Lucas De Marchi7c2ab352011-11-29 18:07:43 -020037
Lucas De Marchi7c2ab352011-11-29 18:07:43 -020038struct kmod_alias {
39 char *name;
Gustavo Sverzut Barbieri43c29d12011-12-04 17:32:44 -020040 char modname[];
Lucas De Marchi7c2ab352011-11-29 18:07:43 -020041};
42
Lucas De Marchi615c42b2011-12-07 03:18:57 -020043struct kmod_options {
44 char *options;
45 char modname[];
46};
47
Lucas De Marchia5cce6d2011-12-07 11:31:28 -020048struct kmod_command {
49 char *command;
50 char modname[];
51};
52
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020053struct 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
Lucas De Marchic1c9c442011-12-24 10:50:47 -020061const char *kmod_blacklist_get_modname(const struct kmod_list *l)
62{
63 return l->data;
64}
65
Lucas De Marchib0ef19f2011-11-30 18:18:13 -020066const char *kmod_alias_get_name(const struct kmod_list *l) {
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -020067 const struct kmod_alias *alias = l->data;
Lucas De Marchib0ef19f2011-11-30 18:18:13 -020068 return alias->name;
69}
70
71const char *kmod_alias_get_modname(const struct kmod_list *l) {
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -020072 const struct kmod_alias *alias = l->data;
Lucas De Marchib0ef19f2011-11-30 18:18:13 -020073 return alias->modname;
74}
75
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020076const char *kmod_option_get_options(const struct kmod_list *l) {
77 const struct kmod_options *alias = l->data;
78 return alias->options;
79}
80
81const char *kmod_option_get_modname(const struct kmod_list *l) {
82 const struct kmod_options *alias = l->data;
83 return alias->modname;
84}
85
86const char *kmod_command_get_command(const struct kmod_list *l) {
87 const struct kmod_command *alias = l->data;
88 return alias->command;
89}
90
91const char *kmod_command_get_modname(const struct kmod_list *l) {
92 const struct kmod_command *alias = l->data;
93 return alias->modname;
94}
95
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020096const char *kmod_softdep_get_name(const struct kmod_list *l) {
97 const struct kmod_softdep *dep = l->data;
98 return dep->name;
99}
100
101const 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
107const 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
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200113static 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{
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200119 _cleanup_free_ struct kmod_command *cmd;
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200120 struct kmod_list *l;
121 size_t modnamelen = strlen(modname) + 1;
122 size_t commandlen = strlen(command) + 1;
123
Lucas De Marchie5a7f6a2011-12-19 14:36:23 -0200124 DBG(config->ctx, "modname='%s' cmd='%s %s'\n", modname, command_name,
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200125 command);
126
127 cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200128 if (!cmd)
129 return -ENOMEM;
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200130
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);
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200136 if (!l)
137 return -ENOMEM;
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200138
139 *list = l;
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200140 cmd = NULL;
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200141 return 0;
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200142}
143
144static 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
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200154static int kmod_config_add_options(struct kmod_config *config,
155 const char *modname, const char *options)
156{
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200157 _cleanup_free_ struct kmod_options *opt;
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200158 struct kmod_list *list;
159 size_t modnamelen = strlen(modname) + 1;
160 size_t optionslen = strlen(options) + 1;
161
Lucas De Marchi23c0d012011-12-14 17:21:46 -0200162 DBG(config->ctx, "modname='%s' options='%s'\n", modname, options);
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200163
164 opt = malloc(sizeof(*opt) + modnamelen + optionslen);
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200165 if (!opt)
166 return -ENOMEM;
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200167
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);
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200175 if (!list)
176 return -ENOMEM;
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200177
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200178 opt = NULL;
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200179 config->options = list;
180 return 0;
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200181}
182
Lucas De Marchic35347f2011-12-12 10:48:02 -0200183static void kmod_config_free_options(struct kmod_config *config,
184 struct kmod_list *l)
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200185{
186 struct kmod_options *opt = l->data;
187
188 free(opt);
189
190 config->options = kmod_list_remove(l);
191}
192
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200193static int kmod_config_add_alias(struct kmod_config *config,
Lucas De Marchic35347f2011-12-12 10:48:02 -0200194 const char *name, const char *modname)
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200195{
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200196 _cleanup_free_ struct kmod_alias *alias;
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200197 struct kmod_list *list;
Gustavo Sverzut Barbieri43c29d12011-12-04 17:32:44 -0200198 size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200199
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200200 DBG(config->ctx, "name=%s modname=%s\n", name, modname);
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200201
Gustavo Sverzut Barbieri43c29d12011-12-04 17:32:44 -0200202 alias = malloc(sizeof(*alias) + namelen + modnamelen);
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200203 if (!alias)
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200204 return -ENOMEM;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200205
Gustavo Sverzut Barbieri43c29d12011-12-04 17:32:44 -0200206 alias->name = sizeof(*alias) + modnamelen + (char *)alias;
207
208 memcpy(alias->modname, modname, modnamelen);
209 memcpy(alias->name, name, namelen);
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200210
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200211 list = kmod_list_append(config->aliases, alias);
212 if (!list)
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200213 return -ENOMEM;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200214
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200215 alias = NULL;
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200216 config->aliases = list;
217 return 0;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200218}
219
Lucas De Marchic35347f2011-12-12 10:48:02 -0200220static void kmod_config_free_alias(struct kmod_config *config,
221 struct kmod_list *l)
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200222{
223 struct kmod_alias *alias = l->data;
224
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200225 free(alias);
226
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200227 config->aliases = kmod_list_remove(l);
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200228}
229
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200230static int kmod_config_add_blacklist(struct kmod_config *config,
Lucas De Marchic35347f2011-12-12 10:48:02 -0200231 const char *modname)
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200232{
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200233 _cleanup_free_ char *p;
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200234 struct kmod_list *list;
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200235
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200236 DBG(config->ctx, "modname=%s\n", modname);
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200237
238 p = strdup(modname);
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200239 if (!p)
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200240 return -ENOMEM;
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200241
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200242 list = kmod_list_append(config->blacklists, p);
243 if (!list)
Lucas De Marchi342e9ce2013-11-14 00:33:28 -0200244 return -ENOMEM;
245
246 p = NULL;
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200247 config->blacklists = list;
248 return 0;
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200249}
250
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200251static void kmod_config_free_blacklist(struct kmod_config *config,
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200252 struct kmod_list *l)
253{
254 free(l->data);
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200255 config->blacklists = kmod_list_remove(l);
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200256}
257
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200258static 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
Lucas De Marchi6b04ef32012-01-13 10:49:31 -0200394static 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
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200463static 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
Lucas De Marchi1684e442011-12-14 17:19:19 -0200470static void kcmdline_parse_result(struct kmod_config *config, char *modname,
471 char *param, char *value)
472{
Lucas De Marchi493dc652013-08-13 22:04:46 -0300473 if (modname == NULL || param == NULL)
Lucas De Marchi1684e442011-12-14 17:19:19 -0200474 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 {
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300487 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);
Lucas De Marchi1684e442011-12-14 17:19:19 -0200492 }
493}
494
495static int kmod_config_parse_kcmdline(struct kmod_config *config)
496{
497 char buf[KCMD_LINE_SIZE];
498 int fd, err;
Lucas De Marchi8df21172017-02-16 08:57:01 -0800499 char *p, *modname, *param = NULL, *value = NULL;
500 bool is_quoted = false, is_module = true;
Lucas De Marchi1684e442011-12-14 17:19:19 -0200501
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -0300502 fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC);
Lucas De Marchidd1cf102012-01-06 19:22:41 -0200503 if (fd < 0) {
504 err = -errno;
505 DBG(config->ctx, "could not open '/proc/cmdline' for reading: %m\n");
506 return err;
507 }
508
Lucas De Marchi1684e442011-12-14 17:19:19 -0200509 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++) {
James Minorf27a2b12017-01-20 17:15:50 -0600518 if (*p == '"') {
519 is_quoted = !is_quoted;
520 continue;
521 }
522 if (is_quoted)
523 continue;
Lucas De Marchi1684e442011-12-14 17:19:19 -0200524 switch (*p) {
525 case ' ':
526 *p = '\0';
Michal Marekaa878542014-03-05 13:27:41 +0100527 if (is_module)
528 kcmdline_parse_result(config, modname, param, value);
Lucas De Marchi1684e442011-12-14 17:19:19 -0200529 param = value = NULL;
530 modname = p + 1;
Lucas De Marchi8df21172017-02-16 08:57:01 -0800531 is_module = true;
Lucas De Marchi1684e442011-12-14 17:19:19 -0200532 break;
533 case '.':
Lucas De Marchi66f32282012-10-08 19:04:16 -0300534 if (param == NULL) {
535 *p = '\0';
536 param = p + 1;
537 }
Lucas De Marchi1684e442011-12-14 17:19:19 -0200538 break;
539 case '=':
Lucas De Marchi135bffd2011-12-20 13:25:24 -0200540 if (param != NULL)
541 value = p + 1;
Michal Marekaa878542014-03-05 13:27:41 +0100542 else
Lucas De Marchi8df21172017-02-16 08:57:01 -0800543 is_module = false;
Lucas De Marchi1684e442011-12-14 17:19:19 -0200544 break;
545 }
546 }
547
548 *p = '\0';
Michal Marekaa878542014-03-05 13:27:41 +0100549 if (is_module)
550 kcmdline_parse_result(config, modname, param, value);
Lucas De Marchi1684e442011-12-14 17:19:19 -0200551
552 return 0;
553}
554
Lucas De Marchib7b7ac22011-12-06 02:26:22 -0200555/*
556 * Take an fd and own it. It will be closed on return. filename is used only
557 * for debug messages
558 */
559static int kmod_config_parse(struct kmod_config *config, int fd,
560 const char *filename)
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200561{
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200562 struct kmod_ctx *ctx = config->ctx;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200563 char *line;
564 FILE *fp;
Lucas De Marchi759214f2011-12-22 01:30:40 -0200565 unsigned int linenum = 0;
Lucas De Marchib7b7ac22011-12-06 02:26:22 -0200566 int err;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200567
Lucas De Marchib7b7ac22011-12-06 02:26:22 -0200568 fp = fdopen(fd, "r");
569 if (fp == NULL) {
570 err = -errno;
Lucas De Marchi050db082012-02-18 03:56:21 -0200571 ERR(config->ctx, "fd %d: %m\n", fd);
Lucas De Marchib7b7ac22011-12-06 02:26:22 -0200572 close(fd);
573 return err;
574 }
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200575
Lucas De Marchiaafd3832014-10-03 03:25:06 -0300576 while ((line = freadline_wrapped(fp, &linenum)) != NULL) {
Lucas De Marchic11e62b2011-12-01 18:59:54 -0200577 char *cmd, *saveptr;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200578
579 if (line[0] == '\0' || line[0] == '#')
580 goto done_next;
581
Lucas De Marchic11e62b2011-12-01 18:59:54 -0200582 cmd = strtok_r(line, "\t ", &saveptr);
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200583 if (cmd == NULL)
584 goto done_next;
585
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200586 if (streq(cmd, "alias")) {
Lucas De Marchic11e62b2011-12-01 18:59:54 -0200587 char *alias = strtok_r(NULL, "\t ", &saveptr);
588 char *modname = strtok_r(NULL, "\t ", &saveptr);
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200589
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300590 if (underscores(alias) < 0 || underscores(modname) < 0)
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200591 goto syntax_error;
592
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300593 kmod_config_add_alias(config, alias, modname);
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200594 } else if (streq(cmd, "blacklist")) {
Lucas De Marchic11e62b2011-12-01 18:59:54 -0200595 char *modname = strtok_r(NULL, "\t ", &saveptr);
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200596
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300597 if (underscores(modname) < 0)
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200598 goto syntax_error;
599
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300600 kmod_config_add_blacklist(config, modname);
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200601 } else if (streq(cmd, "options")) {
602 char *modname = strtok_r(NULL, "\t ", &saveptr);
Lucas De Marchi83121fd2012-01-10 15:00:07 -0200603 char *options = strtok_r(NULL, "\0", &saveptr);
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200604
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300605 if (underscores(modname) < 0 || options == NULL)
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200606 goto syntax_error;
607
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300608 kmod_config_add_options(config, modname, options);
Leandro Pereira40ee8da2011-12-28 15:01:16 -0200609 } else if (streq(cmd, "install")) {
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200610 char *modname = strtok_r(NULL, "\t ", &saveptr);
Lucas De Marchi83121fd2012-01-10 15:00:07 -0200611 char *installcmd = strtok_r(NULL, "\0", &saveptr);
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200612
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300613 if (underscores(modname) < 0 || installcmd == NULL)
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200614 goto syntax_error;
615
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300616 kmod_config_add_command(config, modname, installcmd,
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200617 cmd, &config->install_commands);
Leandro Pereira40ee8da2011-12-28 15:01:16 -0200618 } else if (streq(cmd, "remove")) {
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200619 char *modname = strtok_r(NULL, "\t ", &saveptr);
Lucas De Marchi83121fd2012-01-10 15:00:07 -0200620 char *removecmd = strtok_r(NULL, "\0", &saveptr);
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200621
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300622 if (underscores(modname) < 0 || removecmd == NULL)
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200623 goto syntax_error;
624
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300625 kmod_config_add_command(config, modname, removecmd,
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200626 cmd, &config->remove_commands);
Leandro Pereira40ee8da2011-12-28 15:01:16 -0200627 } else if (streq(cmd, "softdep")) {
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200628 char *modname = strtok_r(NULL, "\t ", &saveptr);
Lucas De Marchi83121fd2012-01-10 15:00:07 -0200629 char *softdeps = strtok_r(NULL, "\0", &saveptr);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200630
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300631 if (underscores(modname) < 0 || softdeps == NULL)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200632 goto syntax_error;
633
Lucas De Marchi52c9c992014-10-09 10:59:08 -0300634 kmod_config_add_softdep(config, modname, softdeps);
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200635 } else if (streq(cmd, "include")
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200636 || streq(cmd, "config")) {
Lucas De Marchi0ad5dd02012-01-11 00:28:12 -0200637 ERR(ctx, "%s: command %s is deprecated and not parsed anymore\n",
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200638 filename, cmd);
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200639 } else {
640syntax_error:
641 ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n",
642 filename, linenum, cmd);
643 }
644
645done_next:
646 free(line);
647 }
648
649 fclose(fp);
650
651 return 0;
652}
653
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200654void kmod_config_free(struct kmod_config *config)
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200655{
656 while (config->aliases)
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200657 kmod_config_free_alias(config, config->aliases);
Lucas De Marchi81cf2062011-11-29 18:48:02 -0200658
659 while (config->blacklists)
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200660 kmod_config_free_blacklist(config, config->blacklists);
661
Lucas De Marchi615c42b2011-12-07 03:18:57 -0200662 while (config->options)
663 kmod_config_free_options(config, config->options);
664
Lucas De Marchia5cce6d2011-12-07 11:31:28 -0200665 while (config->install_commands) {
666 kmod_config_free_command(config, config->install_commands,
667 &config->install_commands);
668 }
669
670 while (config->remove_commands) {
671 kmod_config_free_command(config, config->remove_commands,
672 &config->remove_commands);
673 }
674
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200675 while (config->softdeps)
676 kmod_config_free_softdep(config, config->softdeps);
677
Lucas De Marchib6a4dfb2011-12-31 18:48:05 -0200678 for (; config->paths != NULL;
679 config->paths = kmod_list_remove(config->paths))
680 free(config->paths->data);
681
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200682 free(config);
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200683}
684
Lucas De Marchi98c80f42011-12-12 15:23:03 -0200685static bool conf_files_filter_out(struct kmod_ctx *ctx, DIR *d,
686 const char *path, const char *fn)
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200687{
688 size_t len = strlen(fn);
Lucas De Marchi98c80f42011-12-12 15:23:03 -0200689 struct stat st;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200690
691 if (fn[0] == '.')
Lucas De Marchi8f767e22011-12-12 15:27:57 -0200692 return true;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200693
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200694 if (len < 6 || (!streq(&fn[len - 5], ".conf")
Dave Reisner9070b112011-12-31 11:19:55 -0500695 && !streq(&fn[len - 6], ".alias")))
Lucas De Marchi8f767e22011-12-12 15:27:57 -0200696 return true;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200697
Lucas De Marchi98c80f42011-12-12 15:23:03 -0200698 fstatat(dirfd(d), fn, &st, 0);
699
700 if (S_ISDIR(st.st_mode)) {
701 ERR(ctx, "Directories inside directories are not supported: "
702 "%s/%s\n", path, fn);
Lucas De Marchi8f767e22011-12-12 15:27:57 -0200703 return true;
Lucas De Marchi98c80f42011-12-12 15:23:03 -0200704 }
705
Lucas De Marchi8f767e22011-12-12 15:27:57 -0200706 return false;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200707}
708
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200709struct conf_file {
710 const char *path;
711 bool is_single;
712 char name[];
713};
714
715static int conf_files_insert_sorted(struct kmod_ctx *ctx,
716 struct kmod_list **list,
717 const char *path, const char *name)
718{
719 struct kmod_list *lpos, *tmp;
720 struct conf_file *cf;
721 size_t namelen;
722 int cmp = -1;
723 bool is_single = false;
724
725 if (name == NULL) {
726 name = basename(path);
727 is_single = true;
728 }
729
730 kmod_list_foreach(lpos, *list) {
731 cf = lpos->data;
732
733 if ((cmp = strcmp(name, cf->name)) <= 0)
734 break;
735 }
736
737 if (cmp == 0) {
738 DBG(ctx, "Ignoring duplicate config file: %s/%s\n", path,
739 name);
740 return -EEXIST;
741 }
742
743 namelen = strlen(name);
744 cf = malloc(sizeof(*cf) + namelen + 1);
745 if (cf == NULL)
746 return -ENOMEM;
747
748 memcpy(cf->name, name, namelen + 1);
749 cf->path = path;
750 cf->is_single = is_single;
751
752 if (lpos == NULL)
753 tmp = kmod_list_append(*list, cf);
754 else if (lpos == *list)
755 tmp = kmod_list_prepend(*list, cf);
756 else
757 tmp = kmod_list_insert_before(lpos, cf);
758
759 if (tmp == NULL) {
760 free(cf);
761 return -ENOMEM;
762 }
763
764 if (lpos == NULL || lpos == *list)
765 *list = tmp;
766
767 return 0;
768}
769
Lucas De Marchi47823962011-12-12 15:00:51 -0200770/*
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200771 * Insert configuration files in @list, ignoring duplicates
Lucas De Marchi47823962011-12-12 15:00:51 -0200772 */
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200773static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
Lucas De Marchib6a4dfb2011-12-31 18:48:05 -0200774 const char *path,
775 unsigned long long *path_stamp)
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200776{
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200777 DIR *d;
778 int err;
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200779 struct stat st;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -0300780 struct dirent *dent;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200781
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200782 if (stat(path, &st) != 0) {
783 err = -errno;
784 DBG(ctx, "could not stat '%s': %m\n", path);
785 return err;
786 }
787
Lucas De Marchi6068aaa2012-01-17 12:10:42 -0200788 *path_stamp = stat_mstamp(&st);
Lucas De Marchib6a4dfb2011-12-31 18:48:05 -0200789
Michal Marek519d27d2014-03-04 16:51:25 +0100790 if (!S_ISDIR(st.st_mode)) {
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200791 conf_files_insert_sorted(ctx, list, path, NULL);
792 return 0;
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200793 }
Lucas De Marchi1c250ec2011-12-12 18:36:27 -0200794
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200795 d = opendir(path);
796 if (d == NULL) {
Gustavo Sverzut Barbieridfa96f12012-01-07 19:37:37 -0200797 ERR(ctx, "opendir(%s): %m\n", path);
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200798 return -EINVAL;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200799 }
800
Lucas De Marchi7e0385c2013-08-27 23:29:47 -0300801 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
802 if (conf_files_filter_out(ctx, d, path, dent->d_name))
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200803 continue;
804
Lucas De Marchi7e0385c2013-08-27 23:29:47 -0300805 conf_files_insert_sorted(ctx, list, path, dent->d_name);
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200806 }
807
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200808 closedir(d);
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200809 return 0;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200810}
811
Lucas De Marchic35347f2011-12-12 10:48:02 -0200812int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config,
813 const char * const *config_paths)
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200814{
Gustavo Sverzut Barbierid13e6062011-12-02 21:40:22 -0200815 struct kmod_config *config;
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200816 struct kmod_list *list = NULL;
Lucas De Marchib6a4dfb2011-12-31 18:48:05 -0200817 struct kmod_list *path_list = NULL;
Lucas De Marchib7b7ac22011-12-06 02:26:22 -0200818 size_t i;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200819
Tom Gundersen82403332014-03-31 15:18:51 +0200820 conf_files_insert_sorted(ctx, &list, kmod_get_dirname(ctx), "modules.softdep");
821
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200822 for (i = 0; config_paths[i] != NULL; i++) {
823 const char *path = config_paths[i];
Lucas De Marchib6a4dfb2011-12-31 18:48:05 -0200824 unsigned long long path_stamp = 0;
825 size_t pathlen;
826 struct kmod_list *tmp;
827 struct kmod_config_path *cf;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200828
Lucas De Marchib6a4dfb2011-12-31 18:48:05 -0200829 if (conf_files_list(ctx, &list, path, &path_stamp) < 0)
830 continue;
831
832 pathlen = strlen(path) + 1;
833 cf = malloc(sizeof(*cf) + pathlen);
834 if (cf == NULL)
835 goto oom;
836
837 cf->stamp = path_stamp;
838 memcpy(cf->path, path, pathlen);
839
840 tmp = kmod_list_append(path_list, cf);
841 if (tmp == NULL)
842 goto oom;
843 path_list = tmp;
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200844 }
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200845
Lucas De Marchib6a4dfb2011-12-31 18:48:05 -0200846 *p_config = config = calloc(1, sizeof(struct kmod_config));
847 if (config == NULL)
848 goto oom;
849
850 config->paths = path_list;
Lucas De Marchi29b69c02012-01-04 08:14:54 -0200851 config->ctx = ctx;
Lucas De Marchib6a4dfb2011-12-31 18:48:05 -0200852
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200853 for (; list != NULL; list = kmod_list_remove(list)) {
Lucas De Marchi53d3a992016-08-08 11:42:52 -0300854 char buf[PATH_MAX];
855 const char *fn = buf;
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200856 struct conf_file *cf = list->data;
857 int fd;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200858
Lucas De Marchi53d3a992016-08-08 11:42:52 -0300859 if (cf->is_single) {
860 fn = cf->path;
861 } else if (snprintf(buf, sizeof(buf), "%s/%s",
862 cf->path, cf->name) >= (int)sizeof(buf)) {
863 ERR(ctx, "Error parsing %s/%s: path too long\n",
864 cf->path, cf->name);
865 free(cf);
866 continue;
867 }
Lucas De Marchib7b7ac22011-12-06 02:26:22 -0200868
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200869 fd = open(fn, O_RDONLY|O_CLOEXEC);
870 DBG(ctx, "parsing file '%s' fd=%d\n", fn, fd);
Lucas De Marchib7b7ac22011-12-06 02:26:22 -0200871
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200872 if (fd >= 0)
873 kmod_config_parse(config, fd, fn);
Lucas De Marchib7b7ac22011-12-06 02:26:22 -0200874
Lucas De Marchi7fe5f7a2011-12-20 15:56:31 -0200875 free(cf);
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200876 }
877
Lucas De Marchi1684e442011-12-14 17:19:19 -0200878 kmod_config_parse_kcmdline(config);
879
Lucas De Marchib7b7ac22011-12-06 02:26:22 -0200880 return 0;
Lucas De Marchib6a4dfb2011-12-31 18:48:05 -0200881
882oom:
883 for (; list != NULL; list = kmod_list_remove(list))
884 free(list->data);
885
886 for (; path_list != NULL; path_list = kmod_list_remove(path_list))
887 free(path_list->data);
888
889 return -ENOMEM;
Lucas De Marchi7c2ab352011-11-29 18:07:43 -0200890}
Lucas De Marchi00178622012-01-13 02:24:18 -0200891
892/**********************************************************************
893 * struct kmod_config_iter functions
894 **********************************************************************/
895
896enum config_type {
897 CONFIG_TYPE_BLACKLIST = 0,
898 CONFIG_TYPE_INSTALL,
899 CONFIG_TYPE_REMOVE,
900 CONFIG_TYPE_ALIAS,
901 CONFIG_TYPE_OPTION,
902 CONFIG_TYPE_SOFTDEP,
903};
904
905struct kmod_config_iter {
906 enum config_type type;
Lucas De Marchi6b04ef32012-01-13 10:49:31 -0200907 bool intermediate;
Lucas De Marchi00178622012-01-13 02:24:18 -0200908 const struct kmod_list *list;
909 const struct kmod_list *curr;
Lucas De Marchi6b04ef32012-01-13 10:49:31 -0200910 void *data;
Lucas De Marchi00178622012-01-13 02:24:18 -0200911 const char *(*get_key)(const struct kmod_list *l);
912 const char *(*get_value)(const struct kmod_list *l);
913};
914
Lucas De Marchi6b04ef32012-01-13 10:49:31 -0200915static const char *softdep_get_plain_softdep(const struct kmod_list *l)
916{
917 char *s = softdep_to_char(l->data);
918 return s;
919}
920
Lucas De Marchi00178622012-01-13 02:24:18 -0200921static struct kmod_config_iter *kmod_config_iter_new(const struct kmod_ctx* ctx,
922 enum config_type type)
923{
924 struct kmod_config_iter *iter = calloc(1, sizeof(*iter));
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300925 const struct kmod_config *config = kmod_get_config(ctx);
Lucas De Marchi00178622012-01-13 02:24:18 -0200926
927 if (iter == NULL)
928 return NULL;
929
930 iter->type = type;
931
932 switch (type) {
933 case CONFIG_TYPE_BLACKLIST:
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300934 iter->list = config->blacklists;
Lucas De Marchi00178622012-01-13 02:24:18 -0200935 iter->get_key = kmod_blacklist_get_modname;
936 break;
937 case CONFIG_TYPE_INSTALL:
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300938 iter->list = config->install_commands;
Lucas De Marchi00178622012-01-13 02:24:18 -0200939 iter->get_key = kmod_command_get_modname;
940 iter->get_value = kmod_command_get_command;
941 break;
942 case CONFIG_TYPE_REMOVE:
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300943 iter->list = config->remove_commands;
Lucas De Marchi00178622012-01-13 02:24:18 -0200944 iter->get_key = kmod_command_get_modname;
945 iter->get_value = kmod_command_get_command;
946 break;
947 case CONFIG_TYPE_ALIAS:
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300948 iter->list = config->aliases;
Lucas De Marchi00178622012-01-13 02:24:18 -0200949 iter->get_key = kmod_alias_get_name;
950 iter->get_value = kmod_alias_get_modname;
951 break;
952 case CONFIG_TYPE_OPTION:
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300953 iter->list = config->options;
Lucas De Marchi00178622012-01-13 02:24:18 -0200954 iter->get_key = kmod_option_get_modname;
955 iter->get_value = kmod_option_get_options;
956 break;
957 case CONFIG_TYPE_SOFTDEP:
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300958 iter->list = config->softdeps;
Lucas De Marchi00178622012-01-13 02:24:18 -0200959 iter->get_key = kmod_softdep_get_name;
Lucas De Marchi6b04ef32012-01-13 10:49:31 -0200960 iter->get_value = softdep_get_plain_softdep;
961 iter->intermediate = true;
Lucas De Marchi00178622012-01-13 02:24:18 -0200962 break;
963 }
964
965 return iter;
966}
967
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -0200968/**
969 * SECTION:libkmod-config
970 * @short_description: retrieve current libkmod configuration
971 */
972
973/**
974 * kmod_config_get_blacklists:
975 * @ctx: kmod library context
976 *
977 * Retrieve an iterator to deal with the blacklist maintained inside the
978 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
979 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
980 * be made to initialize the iterator and check if it's valid.
981 *
Lucas De Marchi883d8c42012-04-19 11:08:24 -0300982 * Returns: a new iterator over the blacklists or NULL on failure. Free it
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -0200983 * with kmod_config_iter_free_iter().
984 */
Lucas De Marchi00178622012-01-13 02:24:18 -0200985KMOD_EXPORT struct kmod_config_iter *kmod_config_get_blacklists(const struct kmod_ctx *ctx)
986{
987 if (ctx == NULL)
988 return NULL;;
989
990 return kmod_config_iter_new(ctx, CONFIG_TYPE_BLACKLIST);
991}
992
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -0200993/**
994 * kmod_config_get_install_commands:
995 * @ctx: kmod library context
996 *
997 * Retrieve an iterator to deal with the install commands maintained inside the
998 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
999 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1000 * be made to initialize the iterator and check if it's valid.
1001 *
Lucas De Marchi883d8c42012-04-19 11:08:24 -03001002 * Returns: a new iterator over the install commands or NULL on failure. Free
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001003 * it with kmod_config_iter_free_iter().
1004 */
Lucas De Marchi00178622012-01-13 02:24:18 -02001005KMOD_EXPORT struct kmod_config_iter *kmod_config_get_install_commands(const struct kmod_ctx *ctx)
1006{
1007 if (ctx == NULL)
1008 return NULL;;
1009
1010 return kmod_config_iter_new(ctx, CONFIG_TYPE_INSTALL);
1011}
1012
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001013/**
1014 * kmod_config_get_remove_commands:
1015 * @ctx: kmod library context
1016 *
1017 * Retrieve an iterator to deal with the remove commands maintained inside the
1018 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1019 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1020 * be made to initialize the iterator and check if it's valid.
1021 *
Lucas De Marchi883d8c42012-04-19 11:08:24 -03001022 * Returns: a new iterator over the remove commands or NULL on failure. Free
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001023 * it with kmod_config_iter_free_iter().
1024 */
Lucas De Marchi00178622012-01-13 02:24:18 -02001025KMOD_EXPORT struct kmod_config_iter *kmod_config_get_remove_commands(const struct kmod_ctx *ctx)
1026{
1027 if (ctx == NULL)
1028 return NULL;;
1029
1030 return kmod_config_iter_new(ctx, CONFIG_TYPE_REMOVE);
1031}
1032
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001033/**
1034 * kmod_config_get_aliases:
1035 * @ctx: kmod library context
1036 *
1037 * Retrieve an iterator to deal with the aliases maintained inside the
1038 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1039 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1040 * be made to initialize the iterator and check if it's valid.
1041 *
Lucas De Marchi883d8c42012-04-19 11:08:24 -03001042 * Returns: a new iterator over the aliases or NULL on failure. Free it with
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001043 * kmod_config_iter_free_iter().
1044 */
Lucas De Marchi00178622012-01-13 02:24:18 -02001045KMOD_EXPORT struct kmod_config_iter *kmod_config_get_aliases(const struct kmod_ctx *ctx)
1046{
1047 if (ctx == NULL)
1048 return NULL;;
1049
1050 return kmod_config_iter_new(ctx, CONFIG_TYPE_ALIAS);
1051}
1052
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001053/**
1054 * kmod_config_get_options:
1055 * @ctx: kmod library context
1056 *
1057 * Retrieve an iterator to deal with the options maintained inside the
1058 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1059 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1060 * be made to initialize the iterator and check if it's valid.
1061 *
Lucas De Marchi883d8c42012-04-19 11:08:24 -03001062 * Returns: a new iterator over the options or NULL on failure. Free it with
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001063 * kmod_config_iter_free_iter().
1064 */
Lucas De Marchi00178622012-01-13 02:24:18 -02001065KMOD_EXPORT struct kmod_config_iter *kmod_config_get_options(const struct kmod_ctx *ctx)
1066{
1067 if (ctx == NULL)
1068 return NULL;;
1069
1070 return kmod_config_iter_new(ctx, CONFIG_TYPE_OPTION);
1071}
1072
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001073/**
1074 * kmod_config_get_softdeps:
1075 * @ctx: kmod library context
1076 *
1077 * Retrieve an iterator to deal with the softdeps maintained inside the
1078 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1079 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1080 * be made to initialize the iterator and check if it's valid.
1081 *
Lucas De Marchi883d8c42012-04-19 11:08:24 -03001082 * Returns: a new iterator over the softdeps or NULL on failure. Free it with
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001083 * kmod_config_iter_free_iter().
1084 */
Lucas De Marchi00178622012-01-13 02:24:18 -02001085KMOD_EXPORT struct kmod_config_iter *kmod_config_get_softdeps(const struct kmod_ctx *ctx)
1086{
1087 if (ctx == NULL)
1088 return NULL;;
1089
1090 return kmod_config_iter_new(ctx, CONFIG_TYPE_SOFTDEP);
1091}
1092
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001093/**
1094 * kmod_config_iter_get_key:
1095 * @iter: iterator over a certain configuration
1096 *
1097 * When using a new allocated iterator, user must perform a call to
1098 * kmod_config_iter_next() to initialize iterator's position and check if it's
1099 * valid.
1100 *
1101 * Returns: the key of the current configuration pointed by @iter.
1102 */
Lucas De Marchi00178622012-01-13 02:24:18 -02001103KMOD_EXPORT const char *kmod_config_iter_get_key(const struct kmod_config_iter *iter)
1104{
1105 if (iter == NULL || iter->curr == NULL)
1106 return NULL;
1107
1108 return iter->get_key(iter->curr);
1109}
1110
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001111/**
1112 * kmod_config_iter_get_value:
1113 * @iter: iterator over a certain configuration
1114 *
1115 * When using a new allocated iterator, user must perform a call to
1116 * kmod_config_iter_next() to initialize iterator's position and check if it's
1117 * valid.
1118 *
1119 * Returns: the value of the current configuration pointed by @iter.
1120 */
Lucas De Marchi00178622012-01-13 02:24:18 -02001121KMOD_EXPORT const char *kmod_config_iter_get_value(const struct kmod_config_iter *iter)
1122{
Lucas De Marchi6b04ef32012-01-13 10:49:31 -02001123 const char *s;
1124
Lucas De Marchi00178622012-01-13 02:24:18 -02001125 if (iter == NULL || iter->curr == NULL)
1126 return NULL;
1127
1128 if (iter->get_value == NULL)
1129 return NULL;
1130
Lucas De Marchi6b04ef32012-01-13 10:49:31 -02001131 if (iter->intermediate) {
1132 struct kmod_config_iter *i = (struct kmod_config_iter *)iter;
1133
1134 free(i->data);
1135 s = i->data = (void *) iter->get_value(iter->curr);
1136 } else
1137 s = iter->get_value(iter->curr);
1138
1139 return s;
Lucas De Marchi00178622012-01-13 02:24:18 -02001140}
1141
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001142/**
1143 * kmod_config_iter_next:
1144 * @iter: iterator over a certain configuration
1145 *
1146 * Make @iter point to the next item of a certain configuration. It's an
1147 * automatically recycling iterator. When it reaches the end, false is
1148 * returned; then if user wants to iterate again, it's sufficient to call this
1149 * function once more.
1150 *
1151 * Returns: true if next position of @iter is valid or false if its end is
1152 * reached.
1153 */
Lucas De Marchi00178622012-01-13 02:24:18 -02001154KMOD_EXPORT bool kmod_config_iter_next(struct kmod_config_iter *iter)
1155{
1156 if (iter == NULL)
1157 return false;
1158
1159 if (iter->curr == NULL) {
1160 iter->curr = iter->list;
1161 return iter->curr != NULL;
1162 }
1163
1164 iter->curr = kmod_list_next(iter->list, iter->curr);
1165
1166 return iter->curr != NULL;
1167}
1168
Lucas De Marchi2f47c7f2012-01-14 12:09:34 -02001169/**
1170 * kmod_config_iter_free_iter:
1171 * @iter: iterator over a certain configuration
1172 *
1173 * Free resources used by the iterator.
1174 */
Lucas De Marchi00178622012-01-13 02:24:18 -02001175KMOD_EXPORT void kmod_config_iter_free_iter(struct kmod_config_iter *iter)
1176{
Lucas De Marchi6b04ef32012-01-13 10:49:31 -02001177 free(iter->data);
Lucas De Marchi00178622012-01-13 02:24:18 -02001178 free(iter);
1179}