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, see <http://www.gnu.org/licenses/>.
31 #include <sys/types.h>
33 #include <shared/util.h>
36 #include "libkmod-internal.h"
61 const char *kmod_blacklist_get_modname(const struct kmod_list
*l
)
66 const char *kmod_alias_get_name(const struct kmod_list
*l
) {
67 const struct kmod_alias
*alias
= l
->data
;
71 const char *kmod_alias_get_modname(const struct kmod_list
*l
) {
72 const struct kmod_alias
*alias
= l
->data
;
73 return alias
->modname
;
76 const char *kmod_option_get_options(const struct kmod_list
*l
) {
77 const struct kmod_options
*alias
= l
->data
;
78 return alias
->options
;
81 const char *kmod_option_get_modname(const struct kmod_list
*l
) {
82 const struct kmod_options
*alias
= l
->data
;
83 return alias
->modname
;
86 const char *kmod_command_get_command(const struct kmod_list
*l
) {
87 const struct kmod_command
*alias
= l
->data
;
88 return alias
->command
;
91 const char *kmod_command_get_modname(const struct kmod_list
*l
) {
92 const struct kmod_command
*alias
= l
->data
;
93 return alias
->modname
;
96 const char *kmod_softdep_get_name(const struct kmod_list
*l
) {
97 const struct kmod_softdep
*dep
= l
->data
;
101 const char * const *kmod_softdep_get_pre(const struct kmod_list
*l
, unsigned int *count
) {
102 const struct kmod_softdep
*dep
= l
->data
;
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
;
113 static int kmod_config_add_command(struct kmod_config
*config
,
116 const char *command_name
,
117 struct kmod_list
**list
)
119 _cleanup_free_
struct kmod_command
*cmd
;
121 size_t modnamelen
= strlen(modname
) + 1;
122 size_t commandlen
= strlen(command
) + 1;
124 DBG(config
->ctx
, "modname='%s' cmd='%s %s'\n", modname
, command_name
,
127 cmd
= malloc(sizeof(*cmd
) + modnamelen
+ commandlen
);
131 cmd
->command
= sizeof(*cmd
) + modnamelen
+ (char *)cmd
;
132 memcpy(cmd
->modname
, modname
, modnamelen
);
133 memcpy(cmd
->command
, command
, commandlen
);
135 l
= kmod_list_append(*list
, cmd
);
144 static void kmod_config_free_command(struct kmod_config
*config
,
146 struct kmod_list
**list
)
148 struct kmod_command
*cmd
= l
->data
;
151 *list
= kmod_list_remove(l
);
154 static int kmod_config_add_options(struct kmod_config
*config
,
155 const char *modname
, const char *options
)
157 _cleanup_free_
struct kmod_options
*opt
;
158 struct kmod_list
*list
;
159 size_t modnamelen
= strlen(modname
) + 1;
160 size_t optionslen
= strlen(options
) + 1;
162 DBG(config
->ctx
, "modname='%s' options='%s'\n", modname
, options
);
164 opt
= malloc(sizeof(*opt
) + modnamelen
+ optionslen
);
168 opt
->options
= sizeof(*opt
) + modnamelen
+ (char *)opt
;
170 memcpy(opt
->modname
, modname
, modnamelen
);
171 memcpy(opt
->options
, options
, optionslen
);
172 strchr_replace(opt
->options
, '\t', ' ');
174 list
= kmod_list_append(config
->options
, opt
);
179 config
->options
= list
;
183 static void kmod_config_free_options(struct kmod_config
*config
,
186 struct kmod_options
*opt
= l
->data
;
190 config
->options
= kmod_list_remove(l
);
193 static int kmod_config_add_alias(struct kmod_config
*config
,
194 const char *name
, const char *modname
)
196 _cleanup_free_
struct kmod_alias
*alias
;
197 struct kmod_list
*list
;
198 size_t namelen
= strlen(name
) + 1, modnamelen
= strlen(modname
) + 1;
200 DBG(config
->ctx
, "name=%s modname=%s\n", name
, modname
);
202 alias
= malloc(sizeof(*alias
) + namelen
+ modnamelen
);
206 alias
->name
= sizeof(*alias
) + modnamelen
+ (char *)alias
;
208 memcpy(alias
->modname
, modname
, modnamelen
);
209 memcpy(alias
->name
, name
, namelen
);
211 list
= kmod_list_append(config
->aliases
, alias
);
216 config
->aliases
= list
;
220 static void kmod_config_free_alias(struct kmod_config
*config
,
223 struct kmod_alias
*alias
= l
->data
;
227 config
->aliases
= kmod_list_remove(l
);
230 static int kmod_config_add_blacklist(struct kmod_config
*config
,
233 _cleanup_free_
char *p
;
234 struct kmod_list
*list
;
236 DBG(config
->ctx
, "modname=%s\n", modname
);
242 list
= kmod_list_append(config
->blacklists
, p
);
247 config
->blacklists
= list
;
251 static void kmod_config_free_blacklist(struct kmod_config
*config
,
255 config
->blacklists
= kmod_list_remove(l
);
258 static int kmod_config_add_softdep(struct kmod_config
*config
,
262 struct kmod_list
*list
;
263 struct kmod_softdep
*dep
;
266 unsigned int n_pre
= 0, n_post
= 0;
267 size_t modnamelen
= strlen(modname
) + 1;
269 bool was_space
= false;
270 enum { S_NONE
, S_PRE
, S_POST
} mode
= S_NONE
;
272 DBG(config
->ctx
, "modname=%s\n", modname
);
274 /* analyze and count */
275 for (p
= s
= line
; ; s
++) {
295 if (plen
== sizeof("pre:") - 1 &&
296 memcmp(p
, "pre:", sizeof("pre:") - 1) == 0)
298 else if (plen
== sizeof("post:") - 1 &&
299 memcmp(p
, "post:", sizeof("post:") - 1) == 0)
301 else if (*s
!= '\0' || (*s
== '\0' && !was_space
)) {
305 } else if (mode
== S_POST
) {
315 DBG(config
->ctx
, "%u pre, %u post\n", n_pre
, n_post
);
317 dep
= malloc(sizeof(struct kmod_softdep
) + modnamelen
+
318 n_pre
* sizeof(const char *) +
319 n_post
* sizeof(const char *) +
322 ERR(config
->ctx
, "out-of-memory modname=%s\n", modname
);
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
);
331 memcpy(dep
->name
, modname
, modnamelen
);
334 itr
= dep
->name
+ modnamelen
;
338 for (p
= s
= line
; ; s
++) {
358 if (plen
== sizeof("pre:") - 1 &&
359 memcmp(p
, "pre:", sizeof("pre:") - 1) == 0)
361 else if (plen
== sizeof("post:") - 1 &&
362 memcmp(p
, "post:", sizeof("post:") - 1) == 0)
364 else if (*s
!= '\0' || (*s
== '\0' && !was_space
)) {
366 dep
->pre
[n_pre
] = itr
;
367 memcpy(itr
, p
, plen
);
371 } else if (mode
== S_POST
) {
372 dep
->post
[n_post
] = itr
;
373 memcpy(itr
, p
, plen
);
384 list
= kmod_list_append(config
->softdeps
, dep
);
389 config
->softdeps
= list
;
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
;
403 * Rely on the fact that dep->pre[] and dep->post[] are strv's that
404 * point to a contiguous buffer
406 if (dep
->n_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
;
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
;
424 itr
= s
= malloc(sz
);
431 memcpy(itr
, "pre: ", sz_preprefix
);
434 /* include last '\0' */
435 memcpy(itr
, dep
->pre
[0], sz_pre
+ 1);
436 for (p
= itr
; p
< itr
+ sz_pre
; p
++) {
446 memcpy(itr
, "post: ", sz_postprefix
);
447 itr
+= sz_postprefix
;
449 /* include last '\0' */
450 memcpy(itr
, dep
->post
[0], sz_post
+ 1);
451 for (p
= itr
; p
< itr
+ sz_post
; p
++) {
463 static void kmod_config_free_softdep(struct kmod_config
*config
,
467 config
->softdeps
= kmod_list_remove(l
);
470 static void kcmdline_parse_result(struct kmod_config
*config
, char *modname
,
471 char *param
, char *value
)
473 if (modname
== NULL
|| param
== NULL
)
476 DBG(config
->ctx
, "%s %s\n", modname
, param
);
478 if (streq(modname
, "modprobe") && !strncmp(param
, "blacklist=", 10)) {
480 char *t
= strsep(&value
, ",");
484 kmod_config_add_blacklist(config
, t
);
487 if (underscores(modname
) < 0) {
488 ERR(config
->ctx
, "Ignoring bad option on kernel command line while parsing module name: '%s'\n",
491 kmod_config_add_options(config
, modname
, param
);
495 static int kmod_config_parse_kcmdline(struct kmod_config
*config
)
497 char buf
[KCMD_LINE_SIZE
];
499 char *p
, *modname
, *param
= NULL
, *value
= NULL
;
500 bool is_quoted
= false, is_module
= true;
502 fd
= open("/proc/cmdline", O_RDONLY
|O_CLOEXEC
);
505 DBG(config
->ctx
, "could not open '/proc/cmdline' for reading: %m\n");
509 err
= read_str_safe(fd
, buf
, sizeof(buf
));
512 ERR(config
->ctx
, "could not read from '/proc/cmdline': %s\n",
517 for (p
= buf
, modname
= buf
; *p
!= '\0' && *p
!= '\n'; p
++) {
519 is_quoted
= !is_quoted
;
528 kcmdline_parse_result(config
, modname
, param
, value
);
529 param
= value
= NULL
;
550 kcmdline_parse_result(config
, modname
, param
, value
);
556 * Take an fd and own it. It will be closed on return. filename is used only
559 static int kmod_config_parse(struct kmod_config
*config
, int fd
,
560 const char *filename
)
562 struct kmod_ctx
*ctx
= config
->ctx
;
565 unsigned int linenum
= 0;
568 fp
= fdopen(fd
, "r");
571 ERR(config
->ctx
, "fd %d: %m\n", fd
);
576 while ((line
= freadline_wrapped(fp
, &linenum
)) != NULL
) {
579 if (line
[0] == '\0' || line
[0] == '#')
582 cmd
= strtok_r(line
, "\t ", &saveptr
);
586 if (streq(cmd
, "alias")) {
587 char *alias
= strtok_r(NULL
, "\t ", &saveptr
);
588 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
590 if (underscores(alias
) < 0 || underscores(modname
) < 0)
593 kmod_config_add_alias(config
, alias
, modname
);
594 } else if (streq(cmd
, "blacklist")) {
595 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
597 if (underscores(modname
) < 0)
600 kmod_config_add_blacklist(config
, modname
);
601 } else if (streq(cmd
, "options")) {
602 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
603 char *options
= strtok_r(NULL
, "\0", &saveptr
);
605 if (underscores(modname
) < 0 || options
== NULL
)
608 kmod_config_add_options(config
, modname
, options
);
609 } else if (streq(cmd
, "install")) {
610 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
611 char *installcmd
= strtok_r(NULL
, "\0", &saveptr
);
613 if (underscores(modname
) < 0 || installcmd
== NULL
)
616 kmod_config_add_command(config
, modname
, installcmd
,
617 cmd
, &config
->install_commands
);
618 } else if (streq(cmd
, "remove")) {
619 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
620 char *removecmd
= strtok_r(NULL
, "\0", &saveptr
);
622 if (underscores(modname
) < 0 || removecmd
== NULL
)
625 kmod_config_add_command(config
, modname
, removecmd
,
626 cmd
, &config
->remove_commands
);
627 } else if (streq(cmd
, "softdep")) {
628 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
629 char *softdeps
= strtok_r(NULL
, "\0", &saveptr
);
631 if (underscores(modname
) < 0 || softdeps
== NULL
)
634 kmod_config_add_softdep(config
, modname
, softdeps
);
635 } else if (streq(cmd
, "include")
636 || streq(cmd
, "config")) {
637 ERR(ctx
, "%s: command %s is deprecated and not parsed anymore\n",
641 ERR(ctx
, "%s line %u: ignoring bad line starting with '%s'\n",
642 filename
, linenum
, cmd
);
654 void kmod_config_free(struct kmod_config
*config
)
656 while (config
->aliases
)
657 kmod_config_free_alias(config
, config
->aliases
);
659 while (config
->blacklists
)
660 kmod_config_free_blacklist(config
, config
->blacklists
);
662 while (config
->options
)
663 kmod_config_free_options(config
, config
->options
);
665 while (config
->install_commands
) {
666 kmod_config_free_command(config
, config
->install_commands
,
667 &config
->install_commands
);
670 while (config
->remove_commands
) {
671 kmod_config_free_command(config
, config
->remove_commands
,
672 &config
->remove_commands
);
675 while (config
->softdeps
)
676 kmod_config_free_softdep(config
, config
->softdeps
);
678 for (; config
->paths
!= NULL
;
679 config
->paths
= kmod_list_remove(config
->paths
))
680 free(config
->paths
->data
);
685 static bool conf_files_filter_out(struct kmod_ctx
*ctx
, DIR *d
,
686 const char *path
, const char *fn
)
688 size_t len
= strlen(fn
);
694 if (len
< 6 || (!streq(&fn
[len
- 5], ".conf")
695 && !streq(&fn
[len
- 6], ".alias")))
698 fstatat(dirfd(d
), fn
, &st
, 0);
700 if (S_ISDIR(st
.st_mode
)) {
701 ERR(ctx
, "Directories inside directories are not supported: "
702 "%s/%s\n", path
, fn
);
715 static int conf_files_insert_sorted(struct kmod_ctx
*ctx
,
716 struct kmod_list
**list
,
717 const char *path
, const char *name
)
719 struct kmod_list
*lpos
, *tmp
;
720 struct conf_file
*cf
;
723 bool is_single
= false;
726 name
= basename(path
);
730 kmod_list_foreach(lpos
, *list
) {
733 if ((cmp
= strcmp(name
, cf
->name
)) <= 0)
738 DBG(ctx
, "Ignoring duplicate config file: %s/%s\n", path
,
743 namelen
= strlen(name
);
744 cf
= malloc(sizeof(*cf
) + namelen
+ 1);
748 memcpy(cf
->name
, name
, namelen
+ 1);
750 cf
->is_single
= is_single
;
753 tmp
= kmod_list_append(*list
, cf
);
754 else if (lpos
== *list
)
755 tmp
= kmod_list_prepend(*list
, cf
);
757 tmp
= kmod_list_insert_before(lpos
, cf
);
764 if (lpos
== NULL
|| lpos
== *list
)
771 * Insert configuration files in @list, ignoring duplicates
773 static int conf_files_list(struct kmod_ctx
*ctx
, struct kmod_list
**list
,
775 unsigned long long *path_stamp
)
782 if (stat(path
, &st
) != 0) {
784 DBG(ctx
, "could not stat '%s': %m\n", path
);
788 *path_stamp
= stat_mstamp(&st
);
790 if (!S_ISDIR(st
.st_mode
)) {
791 conf_files_insert_sorted(ctx
, list
, path
, NULL
);
797 ERR(ctx
, "opendir(%s): %m\n", path
);
801 for (dent
= readdir(d
); dent
!= NULL
; dent
= readdir(d
)) {
802 if (conf_files_filter_out(ctx
, d
, path
, dent
->d_name
))
805 conf_files_insert_sorted(ctx
, list
, path
, dent
->d_name
);
812 int kmod_config_new(struct kmod_ctx
*ctx
, struct kmod_config
**p_config
,
813 const char * const *config_paths
)
815 struct kmod_config
*config
;
816 struct kmod_list
*list
= NULL
;
817 struct kmod_list
*path_list
= NULL
;
820 conf_files_insert_sorted(ctx
, &list
, kmod_get_dirname(ctx
), "modules.softdep");
822 for (i
= 0; config_paths
[i
] != NULL
; i
++) {
823 const char *path
= config_paths
[i
];
824 unsigned long long path_stamp
= 0;
826 struct kmod_list
*tmp
;
827 struct kmod_config_path
*cf
;
829 if (conf_files_list(ctx
, &list
, path
, &path_stamp
) < 0)
832 pathlen
= strlen(path
) + 1;
833 cf
= malloc(sizeof(*cf
) + pathlen
);
837 cf
->stamp
= path_stamp
;
838 memcpy(cf
->path
, path
, pathlen
);
840 tmp
= kmod_list_append(path_list
, cf
);
846 *p_config
= config
= calloc(1, sizeof(struct kmod_config
));
850 config
->paths
= path_list
;
853 for (; list
!= NULL
; list
= kmod_list_remove(list
)) {
855 const char *fn
= buf
;
856 struct conf_file
*cf
= list
->data
;
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",
869 fd
= open(fn
, O_RDONLY
|O_CLOEXEC
);
870 DBG(ctx
, "parsing file '%s' fd=%d\n", fn
, fd
);
873 kmod_config_parse(config
, fd
, fn
);
878 kmod_config_parse_kcmdline(config
);
883 for (; list
!= NULL
; list
= kmod_list_remove(list
))
886 for (; path_list
!= NULL
; path_list
= kmod_list_remove(path_list
))
887 free(path_list
->data
);
892 /**********************************************************************
893 * struct kmod_config_iter functions
894 **********************************************************************/
897 CONFIG_TYPE_BLACKLIST
= 0,
905 struct kmod_config_iter
{
906 enum config_type type
;
908 const struct kmod_list
*list
;
909 const struct kmod_list
*curr
;
911 const char *(*get_key
)(const struct kmod_list
*l
);
912 const char *(*get_value
)(const struct kmod_list
*l
);
915 static const char *softdep_get_plain_softdep(const struct kmod_list
*l
)
917 char *s
= softdep_to_char(l
->data
);
921 static struct kmod_config_iter
*kmod_config_iter_new(const struct kmod_ctx
* ctx
,
922 enum config_type type
)
924 struct kmod_config_iter
*iter
= calloc(1, sizeof(*iter
));
925 const struct kmod_config
*config
= kmod_get_config(ctx
);
933 case CONFIG_TYPE_BLACKLIST
:
934 iter
->list
= config
->blacklists
;
935 iter
->get_key
= kmod_blacklist_get_modname
;
937 case CONFIG_TYPE_INSTALL
:
938 iter
->list
= config
->install_commands
;
939 iter
->get_key
= kmod_command_get_modname
;
940 iter
->get_value
= kmod_command_get_command
;
942 case CONFIG_TYPE_REMOVE
:
943 iter
->list
= config
->remove_commands
;
944 iter
->get_key
= kmod_command_get_modname
;
945 iter
->get_value
= kmod_command_get_command
;
947 case CONFIG_TYPE_ALIAS
:
948 iter
->list
= config
->aliases
;
949 iter
->get_key
= kmod_alias_get_name
;
950 iter
->get_value
= kmod_alias_get_modname
;
952 case CONFIG_TYPE_OPTION
:
953 iter
->list
= config
->options
;
954 iter
->get_key
= kmod_option_get_modname
;
955 iter
->get_value
= kmod_option_get_options
;
957 case CONFIG_TYPE_SOFTDEP
:
958 iter
->list
= config
->softdeps
;
959 iter
->get_key
= kmod_softdep_get_name
;
960 iter
->get_value
= softdep_get_plain_softdep
;
961 iter
->intermediate
= true;
969 * SECTION:libkmod-config
970 * @short_description: retrieve current libkmod configuration
974 * kmod_config_get_blacklists:
975 * @ctx: kmod library context
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.
982 * Returns: a new iterator over the blacklists or NULL on failure. Free it
983 * with kmod_config_iter_free_iter().
985 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_blacklists(const struct kmod_ctx
*ctx
)
990 return kmod_config_iter_new(ctx
, CONFIG_TYPE_BLACKLIST
);
994 * kmod_config_get_install_commands:
995 * @ctx: kmod library context
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.
1002 * Returns: a new iterator over the install commands or NULL on failure. Free
1003 * it with kmod_config_iter_free_iter().
1005 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_install_commands(const struct kmod_ctx
*ctx
)
1010 return kmod_config_iter_new(ctx
, CONFIG_TYPE_INSTALL
);
1014 * kmod_config_get_remove_commands:
1015 * @ctx: kmod library context
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.
1022 * Returns: a new iterator over the remove commands or NULL on failure. Free
1023 * it with kmod_config_iter_free_iter().
1025 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_remove_commands(const struct kmod_ctx
*ctx
)
1030 return kmod_config_iter_new(ctx
, CONFIG_TYPE_REMOVE
);
1034 * kmod_config_get_aliases:
1035 * @ctx: kmod library context
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.
1042 * Returns: a new iterator over the aliases or NULL on failure. Free it with
1043 * kmod_config_iter_free_iter().
1045 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_aliases(const struct kmod_ctx
*ctx
)
1050 return kmod_config_iter_new(ctx
, CONFIG_TYPE_ALIAS
);
1054 * kmod_config_get_options:
1055 * @ctx: kmod library context
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.
1062 * Returns: a new iterator over the options or NULL on failure. Free it with
1063 * kmod_config_iter_free_iter().
1065 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_options(const struct kmod_ctx
*ctx
)
1070 return kmod_config_iter_new(ctx
, CONFIG_TYPE_OPTION
);
1074 * kmod_config_get_softdeps:
1075 * @ctx: kmod library context
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.
1082 * Returns: a new iterator over the softdeps or NULL on failure. Free it with
1083 * kmod_config_iter_free_iter().
1085 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_softdeps(const struct kmod_ctx
*ctx
)
1090 return kmod_config_iter_new(ctx
, CONFIG_TYPE_SOFTDEP
);
1094 * kmod_config_iter_get_key:
1095 * @iter: iterator over a certain configuration
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
1101 * Returns: the key of the current configuration pointed by @iter.
1103 KMOD_EXPORT
const char *kmod_config_iter_get_key(const struct kmod_config_iter
*iter
)
1105 if (iter
== NULL
|| iter
->curr
== NULL
)
1108 return iter
->get_key(iter
->curr
);
1112 * kmod_config_iter_get_value:
1113 * @iter: iterator over a certain configuration
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
1119 * Returns: the value of the current configuration pointed by @iter.
1121 KMOD_EXPORT
const char *kmod_config_iter_get_value(const struct kmod_config_iter
*iter
)
1125 if (iter
== NULL
|| iter
->curr
== NULL
)
1128 if (iter
->get_value
== NULL
)
1131 if (iter
->intermediate
) {
1132 struct kmod_config_iter
*i
= (struct kmod_config_iter
*)iter
;
1135 s
= i
->data
= (void *) iter
->get_value(iter
->curr
);
1137 s
= iter
->get_value(iter
->curr
);
1143 * kmod_config_iter_next:
1144 * @iter: iterator over a certain configuration
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.
1151 * Returns: true if next position of @iter is valid or false if its end is
1154 KMOD_EXPORT
bool kmod_config_iter_next(struct kmod_config_iter
*iter
)
1159 if (iter
->curr
== NULL
) {
1160 iter
->curr
= iter
->list
;
1161 return iter
->curr
!= NULL
;
1164 iter
->curr
= kmod_list_next(iter
->list
, iter
->curr
);
1166 return iter
->curr
!= NULL
;
1170 * kmod_config_iter_free_iter:
1171 * @iter: iterator over a certain configuration
1173 * Free resources used by the iterator.
1175 KMOD_EXPORT
void kmod_config_iter_free_iter(struct kmod_config_iter
*iter
)