2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
5 * Copyright (C) 2013 Intel Corporation. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/types.h>
34 #include <shared/util.h>
37 #include "libkmod-internal.h"
62 const char *kmod_blacklist_get_modname(const struct kmod_list
*l
)
67 const char *kmod_alias_get_name(const struct kmod_list
*l
) {
68 const struct kmod_alias
*alias
= l
->data
;
72 const char *kmod_alias_get_modname(const struct kmod_list
*l
) {
73 const struct kmod_alias
*alias
= l
->data
;
74 return alias
->modname
;
77 const char *kmod_option_get_options(const struct kmod_list
*l
) {
78 const struct kmod_options
*alias
= l
->data
;
79 return alias
->options
;
82 const char *kmod_option_get_modname(const struct kmod_list
*l
) {
83 const struct kmod_options
*alias
= l
->data
;
84 return alias
->modname
;
87 const char *kmod_command_get_command(const struct kmod_list
*l
) {
88 const struct kmod_command
*alias
= l
->data
;
89 return alias
->command
;
92 const char *kmod_command_get_modname(const struct kmod_list
*l
) {
93 const struct kmod_command
*alias
= l
->data
;
94 return alias
->modname
;
97 const char *kmod_softdep_get_name(const struct kmod_list
*l
) {
98 const struct kmod_softdep
*dep
= l
->data
;
102 const char * const *kmod_softdep_get_pre(const struct kmod_list
*l
, unsigned int *count
) {
103 const struct kmod_softdep
*dep
= l
->data
;
108 const char * const *kmod_softdep_get_post(const struct kmod_list
*l
, unsigned int *count
) {
109 const struct kmod_softdep
*dep
= l
->data
;
110 *count
= dep
->n_post
;
115 * Replace dashes with underscores.
116 * Dashes inside character range patterns (e.g. [0-9]) are left unchanged.
118 static char *underscores(struct kmod_ctx
*ctx
, char *s
)
125 for (i
= 0; s
[i
]; i
++) {
132 INFO(ctx
, "Unmatched bracket in %s\n", s
);
136 i
+= strcspn(&s
[i
], "]");
138 INFO(ctx
, "Unmatched bracket in %s\n", s
);
145 static int kmod_config_add_command(struct kmod_config
*config
,
148 const char *command_name
,
149 struct kmod_list
**list
)
151 _cleanup_free_
struct kmod_command
*cmd
;
153 size_t modnamelen
= strlen(modname
) + 1;
154 size_t commandlen
= strlen(command
) + 1;
156 DBG(config
->ctx
, "modname='%s' cmd='%s %s'\n", modname
, command_name
,
159 cmd
= malloc(sizeof(*cmd
) + modnamelen
+ commandlen
);
163 cmd
->command
= sizeof(*cmd
) + modnamelen
+ (char *)cmd
;
164 memcpy(cmd
->modname
, modname
, modnamelen
);
165 memcpy(cmd
->command
, command
, commandlen
);
167 l
= kmod_list_append(*list
, cmd
);
176 static void kmod_config_free_command(struct kmod_config
*config
,
178 struct kmod_list
**list
)
180 struct kmod_command
*cmd
= l
->data
;
183 *list
= kmod_list_remove(l
);
186 static int kmod_config_add_options(struct kmod_config
*config
,
187 const char *modname
, const char *options
)
189 _cleanup_free_
struct kmod_options
*opt
;
190 struct kmod_list
*list
;
191 size_t modnamelen
= strlen(modname
) + 1;
192 size_t optionslen
= strlen(options
) + 1;
194 DBG(config
->ctx
, "modname='%s' options='%s'\n", modname
, options
);
196 opt
= malloc(sizeof(*opt
) + modnamelen
+ optionslen
);
200 opt
->options
= sizeof(*opt
) + modnamelen
+ (char *)opt
;
202 memcpy(opt
->modname
, modname
, modnamelen
);
203 memcpy(opt
->options
, options
, optionslen
);
204 strchr_replace(opt
->options
, '\t', ' ');
206 list
= kmod_list_append(config
->options
, opt
);
211 config
->options
= list
;
215 static void kmod_config_free_options(struct kmod_config
*config
,
218 struct kmod_options
*opt
= l
->data
;
222 config
->options
= kmod_list_remove(l
);
225 static int kmod_config_add_alias(struct kmod_config
*config
,
226 const char *name
, const char *modname
)
228 _cleanup_free_
struct kmod_alias
*alias
;
229 struct kmod_list
*list
;
230 size_t namelen
= strlen(name
) + 1, modnamelen
= strlen(modname
) + 1;
232 DBG(config
->ctx
, "name=%s modname=%s\n", name
, modname
);
234 alias
= malloc(sizeof(*alias
) + namelen
+ modnamelen
);
238 alias
->name
= sizeof(*alias
) + modnamelen
+ (char *)alias
;
240 memcpy(alias
->modname
, modname
, modnamelen
);
241 memcpy(alias
->name
, name
, namelen
);
243 list
= kmod_list_append(config
->aliases
, alias
);
248 config
->aliases
= list
;
252 static void kmod_config_free_alias(struct kmod_config
*config
,
255 struct kmod_alias
*alias
= l
->data
;
259 config
->aliases
= kmod_list_remove(l
);
262 static int kmod_config_add_blacklist(struct kmod_config
*config
,
265 _cleanup_free_
char *p
;
266 struct kmod_list
*list
;
268 DBG(config
->ctx
, "modname=%s\n", modname
);
274 list
= kmod_list_append(config
->blacklists
, p
);
279 config
->blacklists
= list
;
283 static void kmod_config_free_blacklist(struct kmod_config
*config
,
287 config
->blacklists
= kmod_list_remove(l
);
290 static int kmod_config_add_softdep(struct kmod_config
*config
,
294 struct kmod_list
*list
;
295 struct kmod_softdep
*dep
;
298 unsigned int n_pre
= 0, n_post
= 0;
299 size_t modnamelen
= strlen(modname
) + 1;
301 bool was_space
= false;
302 enum { S_NONE
, S_PRE
, S_POST
} mode
= S_NONE
;
304 DBG(config
->ctx
, "modname=%s\n", modname
);
306 /* analyze and count */
307 for (p
= s
= line
; ; s
++) {
327 if (plen
== sizeof("pre:") - 1 &&
328 memcmp(p
, "pre:", sizeof("pre:") - 1) == 0)
330 else if (plen
== sizeof("post:") - 1 &&
331 memcmp(p
, "post:", sizeof("post:") - 1) == 0)
333 else if (*s
!= '\0' || (*s
== '\0' && !was_space
)) {
337 } else if (mode
== S_POST
) {
347 DBG(config
->ctx
, "%u pre, %u post\n", n_pre
, n_post
);
349 dep
= malloc(sizeof(struct kmod_softdep
) + modnamelen
+
350 n_pre
* sizeof(const char *) +
351 n_post
* sizeof(const char *) +
354 ERR(config
->ctx
, "out-of-memory modname=%s\n", modname
);
358 dep
->n_post
= n_post
;
359 dep
->pre
= (const char **)((char *)dep
+ sizeof(struct kmod_softdep
));
360 dep
->post
= dep
->pre
+ n_pre
;
361 dep
->name
= (char *)(dep
->post
+ n_post
);
363 memcpy(dep
->name
, modname
, modnamelen
);
366 itr
= dep
->name
+ modnamelen
;
370 for (p
= s
= line
; ; s
++) {
390 if (plen
== sizeof("pre:") - 1 &&
391 memcmp(p
, "pre:", sizeof("pre:") - 1) == 0)
393 else if (plen
== sizeof("post:") - 1 &&
394 memcmp(p
, "post:", sizeof("post:") - 1) == 0)
396 else if (*s
!= '\0' || (*s
== '\0' && !was_space
)) {
398 dep
->pre
[n_pre
] = itr
;
399 memcpy(itr
, p
, plen
);
403 } else if (mode
== S_POST
) {
404 dep
->post
[n_post
] = itr
;
405 memcpy(itr
, p
, plen
);
416 list
= kmod_list_append(config
->softdeps
, dep
);
421 config
->softdeps
= list
;
426 static char *softdep_to_char(struct kmod_softdep
*dep
) {
427 const size_t sz_preprefix
= sizeof("pre: ") - 1;
428 const size_t sz_postprefix
= sizeof("post: ") - 1;
429 size_t sz
= 1; /* at least '\0' */
430 size_t sz_pre
, sz_post
;
431 const char *start
, *end
;
435 * Rely on the fact that dep->pre[] and dep->post[] are strv's that
436 * point to a contiguous buffer
438 if (dep
->n_pre
> 0) {
440 end
= dep
->pre
[dep
->n_pre
- 1]
441 + strlen(dep
->pre
[dep
->n_pre
- 1]);
442 sz_pre
= end
- start
;
443 sz
+= sz_pre
+ sz_preprefix
;
447 if (dep
->n_post
> 0) {
448 start
= dep
->post
[0];
449 end
= dep
->post
[dep
->n_post
- 1]
450 + strlen(dep
->post
[dep
->n_post
- 1]);
451 sz_post
= end
- start
;
452 sz
+= sz_post
+ sz_postprefix
;
456 itr
= s
= malloc(sz
);
463 memcpy(itr
, "pre: ", sz_preprefix
);
466 /* include last '\0' */
467 memcpy(itr
, dep
->pre
[0], sz_pre
+ 1);
468 for (p
= itr
; p
< itr
+ sz_pre
; p
++) {
478 memcpy(itr
, "post: ", sz_postprefix
);
479 itr
+= sz_postprefix
;
481 /* include last '\0' */
482 memcpy(itr
, dep
->post
[0], sz_post
+ 1);
483 for (p
= itr
; p
< itr
+ sz_post
; p
++) {
495 static void kmod_config_free_softdep(struct kmod_config
*config
,
499 config
->softdeps
= kmod_list_remove(l
);
502 static void kcmdline_parse_result(struct kmod_config
*config
, char *modname
,
503 char *param
, char *value
)
505 if (modname
== NULL
|| param
== NULL
)
508 DBG(config
->ctx
, "%s %s\n", modname
, param
);
510 if (streq(modname
, "modprobe") && !strncmp(param
, "blacklist=", 10)) {
512 char *t
= strsep(&value
, ",");
516 kmod_config_add_blacklist(config
, t
);
519 kmod_config_add_options(config
,
520 underscores(config
->ctx
, modname
), param
);
524 static int kmod_config_parse_kcmdline(struct kmod_config
*config
)
526 char buf
[KCMD_LINE_SIZE
];
528 char *p
, *modname
, *param
= NULL
, *value
= NULL
, is_module
= 1;
530 fd
= open("/proc/cmdline", O_RDONLY
|O_CLOEXEC
);
533 DBG(config
->ctx
, "could not open '/proc/cmdline' for reading: %m\n");
537 err
= read_str_safe(fd
, buf
, sizeof(buf
));
540 ERR(config
->ctx
, "could not read from '/proc/cmdline': %s\n",
545 for (p
= buf
, modname
= buf
; *p
!= '\0' && *p
!= '\n'; p
++) {
550 kcmdline_parse_result(config
, modname
, param
, value
);
551 param
= value
= NULL
;
572 kcmdline_parse_result(config
, modname
, param
, value
);
578 * Take an fd and own it. It will be closed on return. filename is used only
581 static int kmod_config_parse(struct kmod_config
*config
, int fd
,
582 const char *filename
)
584 struct kmod_ctx
*ctx
= config
->ctx
;
587 unsigned int linenum
= 0;
590 fp
= fdopen(fd
, "r");
593 ERR(config
->ctx
, "fd %d: %m\n", fd
);
598 while ((line
= freadline_wrapped(fp
, &linenum
)) != NULL
) {
601 if (line
[0] == '\0' || line
[0] == '#')
604 cmd
= strtok_r(line
, "\t ", &saveptr
);
608 if (streq(cmd
, "alias")) {
609 char *alias
= strtok_r(NULL
, "\t ", &saveptr
);
610 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
612 if (alias
== NULL
|| modname
== NULL
)
615 kmod_config_add_alias(config
,
616 underscores(ctx
, alias
),
617 underscores(ctx
, modname
));
618 } else if (streq(cmd
, "blacklist")) {
619 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
624 kmod_config_add_blacklist(config
,
625 underscores(ctx
, modname
));
626 } else if (streq(cmd
, "options")) {
627 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
628 char *options
= strtok_r(NULL
, "\0", &saveptr
);
630 if (modname
== NULL
|| options
== NULL
)
633 kmod_config_add_options(config
,
634 underscores(ctx
, modname
),
636 } else if (streq(cmd
, "install")) {
637 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
638 char *installcmd
= strtok_r(NULL
, "\0", &saveptr
);
640 if (modname
== NULL
|| installcmd
== NULL
)
643 kmod_config_add_command(config
,
644 underscores(ctx
, modname
),
646 cmd
, &config
->install_commands
);
647 } else if (streq(cmd
, "remove")) {
648 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
649 char *removecmd
= strtok_r(NULL
, "\0", &saveptr
);
651 if (modname
== NULL
|| removecmd
== NULL
)
654 kmod_config_add_command(config
,
655 underscores(ctx
, modname
),
657 cmd
, &config
->remove_commands
);
658 } else if (streq(cmd
, "softdep")) {
659 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
660 char *softdeps
= strtok_r(NULL
, "\0", &saveptr
);
662 if (modname
== NULL
|| softdeps
== NULL
)
665 kmod_config_add_softdep(config
,
666 underscores(ctx
, modname
),
668 } else if (streq(cmd
, "include")
669 || streq(cmd
, "config")) {
670 ERR(ctx
, "%s: command %s is deprecated and not parsed anymore\n",
674 ERR(ctx
, "%s line %u: ignoring bad line starting with '%s'\n",
675 filename
, linenum
, cmd
);
687 void kmod_config_free(struct kmod_config
*config
)
689 while (config
->aliases
)
690 kmod_config_free_alias(config
, config
->aliases
);
692 while (config
->blacklists
)
693 kmod_config_free_blacklist(config
, config
->blacklists
);
695 while (config
->options
)
696 kmod_config_free_options(config
, config
->options
);
698 while (config
->install_commands
) {
699 kmod_config_free_command(config
, config
->install_commands
,
700 &config
->install_commands
);
703 while (config
->remove_commands
) {
704 kmod_config_free_command(config
, config
->remove_commands
,
705 &config
->remove_commands
);
708 while (config
->softdeps
)
709 kmod_config_free_softdep(config
, config
->softdeps
);
711 for (; config
->paths
!= NULL
;
712 config
->paths
= kmod_list_remove(config
->paths
))
713 free(config
->paths
->data
);
718 static bool conf_files_filter_out(struct kmod_ctx
*ctx
, DIR *d
,
719 const char *path
, const char *fn
)
721 size_t len
= strlen(fn
);
727 if (len
< 6 || (!streq(&fn
[len
- 5], ".conf")
728 && !streq(&fn
[len
- 6], ".alias")))
731 fstatat(dirfd(d
), fn
, &st
, 0);
733 if (S_ISDIR(st
.st_mode
)) {
734 ERR(ctx
, "Directories inside directories are not supported: "
735 "%s/%s\n", path
, fn
);
748 static int conf_files_insert_sorted(struct kmod_ctx
*ctx
,
749 struct kmod_list
**list
,
750 const char *path
, const char *name
)
752 struct kmod_list
*lpos
, *tmp
;
753 struct conf_file
*cf
;
756 bool is_single
= false;
759 name
= basename(path
);
763 kmod_list_foreach(lpos
, *list
) {
766 if ((cmp
= strcmp(name
, cf
->name
)) <= 0)
771 DBG(ctx
, "Ignoring duplicate config file: %s/%s\n", path
,
776 namelen
= strlen(name
);
777 cf
= malloc(sizeof(*cf
) + namelen
+ 1);
781 memcpy(cf
->name
, name
, namelen
+ 1);
783 cf
->is_single
= is_single
;
786 tmp
= kmod_list_append(*list
, cf
);
787 else if (lpos
== *list
)
788 tmp
= kmod_list_prepend(*list
, cf
);
790 tmp
= kmod_list_insert_before(lpos
, cf
);
797 if (lpos
== NULL
|| lpos
== *list
)
804 * Insert configuration files in @list, ignoring duplicates
806 static int conf_files_list(struct kmod_ctx
*ctx
, struct kmod_list
**list
,
808 unsigned long long *path_stamp
)
815 if (stat(path
, &st
) != 0) {
817 DBG(ctx
, "could not stat '%s': %m\n", path
);
821 *path_stamp
= stat_mstamp(&st
);
823 if (!S_ISDIR(st
.st_mode
)) {
824 conf_files_insert_sorted(ctx
, list
, path
, NULL
);
830 ERR(ctx
, "opendir(%s): %m\n", path
);
834 for (dent
= readdir(d
); dent
!= NULL
; dent
= readdir(d
)) {
835 if (conf_files_filter_out(ctx
, d
, path
, dent
->d_name
))
838 conf_files_insert_sorted(ctx
, list
, path
, dent
->d_name
);
845 int kmod_config_new(struct kmod_ctx
*ctx
, struct kmod_config
**p_config
,
846 const char * const *config_paths
)
848 struct kmod_config
*config
;
849 struct kmod_list
*list
= NULL
;
850 struct kmod_list
*path_list
= NULL
;
853 conf_files_insert_sorted(ctx
, &list
, kmod_get_dirname(ctx
), "modules.softdep");
855 for (i
= 0; config_paths
[i
] != NULL
; i
++) {
856 const char *path
= config_paths
[i
];
857 unsigned long long path_stamp
= 0;
859 struct kmod_list
*tmp
;
860 struct kmod_config_path
*cf
;
862 if (conf_files_list(ctx
, &list
, path
, &path_stamp
) < 0)
865 pathlen
= strlen(path
) + 1;
866 cf
= malloc(sizeof(*cf
) + pathlen
);
870 cf
->stamp
= path_stamp
;
871 memcpy(cf
->path
, path
, pathlen
);
873 tmp
= kmod_list_append(path_list
, cf
);
879 *p_config
= config
= calloc(1, sizeof(struct kmod_config
));
883 config
->paths
= path_list
;
886 for (; list
!= NULL
; list
= kmod_list_remove(list
)) {
888 struct conf_file
*cf
= list
->data
;
892 strcpy(fn
, cf
->path
);
894 snprintf(fn
, sizeof(fn
),"%s/%s", cf
->path
,
897 fd
= open(fn
, O_RDONLY
|O_CLOEXEC
);
898 DBG(ctx
, "parsing file '%s' fd=%d\n", fn
, fd
);
901 kmod_config_parse(config
, fd
, fn
);
906 kmod_config_parse_kcmdline(config
);
911 for (; list
!= NULL
; list
= kmod_list_remove(list
))
914 for (; path_list
!= NULL
; path_list
= kmod_list_remove(path_list
))
915 free(path_list
->data
);
920 /**********************************************************************
921 * struct kmod_config_iter functions
922 **********************************************************************/
925 CONFIG_TYPE_BLACKLIST
= 0,
933 struct kmod_config_iter
{
934 enum config_type type
;
936 const struct kmod_list
*list
;
937 const struct kmod_list
*curr
;
939 const char *(*get_key
)(const struct kmod_list
*l
);
940 const char *(*get_value
)(const struct kmod_list
*l
);
943 static const char *softdep_get_plain_softdep(const struct kmod_list
*l
)
945 char *s
= softdep_to_char(l
->data
);
949 static struct kmod_config_iter
*kmod_config_iter_new(const struct kmod_ctx
* ctx
,
950 enum config_type type
)
952 struct kmod_config_iter
*iter
= calloc(1, sizeof(*iter
));
953 const struct kmod_config
*config
= kmod_get_config(ctx
);
961 case CONFIG_TYPE_BLACKLIST
:
962 iter
->list
= config
->blacklists
;
963 iter
->get_key
= kmod_blacklist_get_modname
;
965 case CONFIG_TYPE_INSTALL
:
966 iter
->list
= config
->install_commands
;
967 iter
->get_key
= kmod_command_get_modname
;
968 iter
->get_value
= kmod_command_get_command
;
970 case CONFIG_TYPE_REMOVE
:
971 iter
->list
= config
->remove_commands
;
972 iter
->get_key
= kmod_command_get_modname
;
973 iter
->get_value
= kmod_command_get_command
;
975 case CONFIG_TYPE_ALIAS
:
976 iter
->list
= config
->aliases
;
977 iter
->get_key
= kmod_alias_get_name
;
978 iter
->get_value
= kmod_alias_get_modname
;
980 case CONFIG_TYPE_OPTION
:
981 iter
->list
= config
->options
;
982 iter
->get_key
= kmod_option_get_modname
;
983 iter
->get_value
= kmod_option_get_options
;
985 case CONFIG_TYPE_SOFTDEP
:
986 iter
->list
= config
->softdeps
;
987 iter
->get_key
= kmod_softdep_get_name
;
988 iter
->get_value
= softdep_get_plain_softdep
;
989 iter
->intermediate
= true;
997 * SECTION:libkmod-config
998 * @short_description: retrieve current libkmod configuration
1002 * kmod_config_get_blacklists:
1003 * @ctx: kmod library context
1005 * Retrieve an iterator to deal with the blacklist 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.
1010 * Returns: a new iterator over the blacklists or NULL on failure. Free it
1011 * with kmod_config_iter_free_iter().
1013 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_blacklists(const struct kmod_ctx
*ctx
)
1018 return kmod_config_iter_new(ctx
, CONFIG_TYPE_BLACKLIST
);
1022 * kmod_config_get_install_commands:
1023 * @ctx: kmod library context
1025 * Retrieve an iterator to deal with the install commands 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.
1030 * Returns: a new iterator over the install commands or NULL on failure. Free
1031 * it with kmod_config_iter_free_iter().
1033 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_install_commands(const struct kmod_ctx
*ctx
)
1038 return kmod_config_iter_new(ctx
, CONFIG_TYPE_INSTALL
);
1042 * kmod_config_get_remove_commands:
1043 * @ctx: kmod library context
1045 * Retrieve an iterator to deal with the remove commands 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.
1050 * Returns: a new iterator over the remove commands or NULL on failure. Free
1051 * it with kmod_config_iter_free_iter().
1053 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_remove_commands(const struct kmod_ctx
*ctx
)
1058 return kmod_config_iter_new(ctx
, CONFIG_TYPE_REMOVE
);
1062 * kmod_config_get_aliases:
1063 * @ctx: kmod library context
1065 * Retrieve an iterator to deal with the aliases 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.
1070 * Returns: a new iterator over the aliases or NULL on failure. Free it with
1071 * kmod_config_iter_free_iter().
1073 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_aliases(const struct kmod_ctx
*ctx
)
1078 return kmod_config_iter_new(ctx
, CONFIG_TYPE_ALIAS
);
1082 * kmod_config_get_options:
1083 * @ctx: kmod library context
1085 * Retrieve an iterator to deal with the options maintained inside the
1086 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1087 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1088 * be made to initialize the iterator and check if it's valid.
1090 * Returns: a new iterator over the options or NULL on failure. Free it with
1091 * kmod_config_iter_free_iter().
1093 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_options(const struct kmod_ctx
*ctx
)
1098 return kmod_config_iter_new(ctx
, CONFIG_TYPE_OPTION
);
1102 * kmod_config_get_softdeps:
1103 * @ctx: kmod library context
1105 * Retrieve an iterator to deal with the softdeps maintained inside the
1106 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1107 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1108 * be made to initialize the iterator and check if it's valid.
1110 * Returns: a new iterator over the softdeps or NULL on failure. Free it with
1111 * kmod_config_iter_free_iter().
1113 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_softdeps(const struct kmod_ctx
*ctx
)
1118 return kmod_config_iter_new(ctx
, CONFIG_TYPE_SOFTDEP
);
1122 * kmod_config_iter_get_key:
1123 * @iter: iterator over a certain configuration
1125 * When using a new allocated iterator, user must perform a call to
1126 * kmod_config_iter_next() to initialize iterator's position and check if it's
1129 * Returns: the key of the current configuration pointed by @iter.
1131 KMOD_EXPORT
const char *kmod_config_iter_get_key(const struct kmod_config_iter
*iter
)
1133 if (iter
== NULL
|| iter
->curr
== NULL
)
1136 return iter
->get_key(iter
->curr
);
1140 * kmod_config_iter_get_value:
1141 * @iter: iterator over a certain configuration
1143 * When using a new allocated iterator, user must perform a call to
1144 * kmod_config_iter_next() to initialize iterator's position and check if it's
1147 * Returns: the value of the current configuration pointed by @iter.
1149 KMOD_EXPORT
const char *kmod_config_iter_get_value(const struct kmod_config_iter
*iter
)
1153 if (iter
== NULL
|| iter
->curr
== NULL
)
1156 if (iter
->get_value
== NULL
)
1159 if (iter
->intermediate
) {
1160 struct kmod_config_iter
*i
= (struct kmod_config_iter
*)iter
;
1163 s
= i
->data
= (void *) iter
->get_value(iter
->curr
);
1165 s
= iter
->get_value(iter
->curr
);
1171 * kmod_config_iter_next:
1172 * @iter: iterator over a certain configuration
1174 * Make @iter point to the next item of a certain configuration. It's an
1175 * automatically recycling iterator. When it reaches the end, false is
1176 * returned; then if user wants to iterate again, it's sufficient to call this
1177 * function once more.
1179 * Returns: true if next position of @iter is valid or false if its end is
1182 KMOD_EXPORT
bool kmod_config_iter_next(struct kmod_config_iter
*iter
)
1187 if (iter
->curr
== NULL
) {
1188 iter
->curr
= iter
->list
;
1189 return iter
->curr
!= NULL
;
1192 iter
->curr
= kmod_list_next(iter
->list
, iter
->curr
);
1194 return iter
->curr
!= NULL
;
1198 * kmod_config_iter_free_iter:
1199 * @iter: iterator over a certain configuration
1201 * Free resources used by the iterator.
1203 KMOD_EXPORT
void kmod_config_iter_free_iter(struct kmod_config_iter
*iter
)