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
31 #include <sys/types.h>
35 #include "libkmod-internal.h"
60 const char *kmod_blacklist_get_modname(const struct kmod_list
*l
)
65 const char *kmod_alias_get_name(const struct kmod_list
*l
) {
66 const struct kmod_alias
*alias
= l
->data
;
70 const char *kmod_alias_get_modname(const struct kmod_list
*l
) {
71 const struct kmod_alias
*alias
= l
->data
;
72 return alias
->modname
;
75 const char *kmod_option_get_options(const struct kmod_list
*l
) {
76 const struct kmod_options
*alias
= l
->data
;
77 return alias
->options
;
80 const char *kmod_option_get_modname(const struct kmod_list
*l
) {
81 const struct kmod_options
*alias
= l
->data
;
82 return alias
->modname
;
85 const char *kmod_command_get_command(const struct kmod_list
*l
) {
86 const struct kmod_command
*alias
= l
->data
;
87 return alias
->command
;
90 const char *kmod_command_get_modname(const struct kmod_list
*l
) {
91 const struct kmod_command
*alias
= l
->data
;
92 return alias
->modname
;
95 const char *kmod_softdep_get_name(const struct kmod_list
*l
) {
96 const struct kmod_softdep
*dep
= l
->data
;
100 const char * const *kmod_softdep_get_pre(const struct kmod_list
*l
, unsigned int *count
) {
101 const struct kmod_softdep
*dep
= l
->data
;
106 const char * const *kmod_softdep_get_post(const struct kmod_list
*l
, unsigned int *count
) {
107 const struct kmod_softdep
*dep
= l
->data
;
108 *count
= dep
->n_post
;
113 * Replace dashes with underscores.
114 * Dashes inside character range patterns (e.g. [0-9]) are left unchanged.
116 static char *underscores(struct kmod_ctx
*ctx
, char *s
)
123 for (i
= 0; s
[i
]; i
++) {
130 INFO(ctx
, "Unmatched bracket in %s\n", s
);
134 i
+= strcspn(&s
[i
], "]");
136 INFO(ctx
, "Unmatched bracket in %s\n", s
);
143 static int kmod_config_add_command(struct kmod_config
*config
,
146 const char *command_name
,
147 struct kmod_list
**list
)
149 _cleanup_free_
struct kmod_command
*cmd
;
151 size_t modnamelen
= strlen(modname
) + 1;
152 size_t commandlen
= strlen(command
) + 1;
154 DBG(config
->ctx
, "modname='%s' cmd='%s %s'\n", modname
, command_name
,
157 cmd
= malloc(sizeof(*cmd
) + modnamelen
+ commandlen
);
161 cmd
->command
= sizeof(*cmd
) + modnamelen
+ (char *)cmd
;
162 memcpy(cmd
->modname
, modname
, modnamelen
);
163 memcpy(cmd
->command
, command
, commandlen
);
165 l
= kmod_list_append(*list
, cmd
);
174 static void kmod_config_free_command(struct kmod_config
*config
,
176 struct kmod_list
**list
)
178 struct kmod_command
*cmd
= l
->data
;
181 *list
= kmod_list_remove(l
);
184 static int kmod_config_add_options(struct kmod_config
*config
,
185 const char *modname
, const char *options
)
187 _cleanup_free_
struct kmod_options
*opt
;
188 struct kmod_list
*list
;
189 size_t modnamelen
= strlen(modname
) + 1;
190 size_t optionslen
= strlen(options
) + 1;
192 DBG(config
->ctx
, "modname='%s' options='%s'\n", modname
, options
);
194 opt
= malloc(sizeof(*opt
) + modnamelen
+ optionslen
);
198 opt
->options
= sizeof(*opt
) + modnamelen
+ (char *)opt
;
200 memcpy(opt
->modname
, modname
, modnamelen
);
201 memcpy(opt
->options
, options
, optionslen
);
202 strchr_replace(opt
->options
, '\t', ' ');
204 list
= kmod_list_append(config
->options
, opt
);
209 config
->options
= list
;
213 static void kmod_config_free_options(struct kmod_config
*config
,
216 struct kmod_options
*opt
= l
->data
;
220 config
->options
= kmod_list_remove(l
);
223 static int kmod_config_add_alias(struct kmod_config
*config
,
224 const char *name
, const char *modname
)
226 _cleanup_free_
struct kmod_alias
*alias
;
227 struct kmod_list
*list
;
228 size_t namelen
= strlen(name
) + 1, modnamelen
= strlen(modname
) + 1;
230 DBG(config
->ctx
, "name=%s modname=%s\n", name
, modname
);
232 alias
= malloc(sizeof(*alias
) + namelen
+ modnamelen
);
236 alias
->name
= sizeof(*alias
) + modnamelen
+ (char *)alias
;
238 memcpy(alias
->modname
, modname
, modnamelen
);
239 memcpy(alias
->name
, name
, namelen
);
241 list
= kmod_list_append(config
->aliases
, alias
);
246 config
->aliases
= list
;
250 static void kmod_config_free_alias(struct kmod_config
*config
,
253 struct kmod_alias
*alias
= l
->data
;
257 config
->aliases
= kmod_list_remove(l
);
260 static int kmod_config_add_blacklist(struct kmod_config
*config
,
263 _cleanup_free_
char *p
;
264 struct kmod_list
*list
;
266 DBG(config
->ctx
, "modname=%s\n", modname
);
272 list
= kmod_list_append(config
->blacklists
, p
);
277 config
->blacklists
= list
;
281 static void kmod_config_free_blacklist(struct kmod_config
*config
,
285 config
->blacklists
= kmod_list_remove(l
);
288 static int kmod_config_add_softdep(struct kmod_config
*config
,
292 struct kmod_list
*list
;
293 struct kmod_softdep
*dep
;
296 unsigned int n_pre
= 0, n_post
= 0;
297 size_t modnamelen
= strlen(modname
) + 1;
299 bool was_space
= false;
300 enum { S_NONE
, S_PRE
, S_POST
} mode
= S_NONE
;
302 DBG(config
->ctx
, "modname=%s\n", modname
);
304 /* analyze and count */
305 for (p
= s
= line
; ; s
++) {
325 if (plen
== sizeof("pre:") - 1 &&
326 memcmp(p
, "pre:", sizeof("pre:") - 1) == 0)
328 else if (plen
== sizeof("post:") - 1 &&
329 memcmp(p
, "post:", sizeof("post:") - 1) == 0)
331 else if (*s
!= '\0' || (*s
== '\0' && !was_space
)) {
335 } else if (mode
== S_POST
) {
345 DBG(config
->ctx
, "%u pre, %u post\n", n_pre
, n_post
);
347 dep
= malloc(sizeof(struct kmod_softdep
) + modnamelen
+
348 n_pre
* sizeof(const char *) +
349 n_post
* sizeof(const char *) +
352 ERR(config
->ctx
, "out-of-memory modname=%s\n", modname
);
356 dep
->n_post
= n_post
;
357 dep
->pre
= (const char **)((char *)dep
+ sizeof(struct kmod_softdep
));
358 dep
->post
= dep
->pre
+ n_pre
;
359 dep
->name
= (char *)(dep
->post
+ n_post
);
361 memcpy(dep
->name
, modname
, modnamelen
);
364 itr
= dep
->name
+ modnamelen
;
368 for (p
= s
= line
; ; s
++) {
388 if (plen
== sizeof("pre:") - 1 &&
389 memcmp(p
, "pre:", sizeof("pre:") - 1) == 0)
391 else if (plen
== sizeof("post:") - 1 &&
392 memcmp(p
, "post:", sizeof("post:") - 1) == 0)
394 else if (*s
!= '\0' || (*s
== '\0' && !was_space
)) {
396 dep
->pre
[n_pre
] = itr
;
397 memcpy(itr
, p
, plen
);
401 } else if (mode
== S_POST
) {
402 dep
->post
[n_post
] = itr
;
403 memcpy(itr
, p
, plen
);
414 list
= kmod_list_append(config
->softdeps
, dep
);
419 config
->softdeps
= list
;
424 static char *softdep_to_char(struct kmod_softdep
*dep
) {
425 const size_t sz_preprefix
= sizeof("pre: ") - 1;
426 const size_t sz_postprefix
= sizeof("post: ") - 1;
427 size_t sz
= 1; /* at least '\0' */
428 size_t sz_pre
, sz_post
;
429 const char *start
, *end
;
433 * Rely on the fact that dep->pre[] and dep->post[] are strv's that
434 * point to a contiguous buffer
436 if (dep
->n_pre
> 0) {
438 end
= dep
->pre
[dep
->n_pre
- 1]
439 + strlen(dep
->pre
[dep
->n_pre
- 1]);
440 sz_pre
= end
- start
;
441 sz
+= sz_pre
+ sz_preprefix
;
445 if (dep
->n_post
> 0) {
446 start
= dep
->post
[0];
447 end
= dep
->post
[dep
->n_post
- 1]
448 + strlen(dep
->post
[dep
->n_post
- 1]);
449 sz_post
= end
- start
;
450 sz
+= sz_post
+ sz_postprefix
;
454 itr
= s
= malloc(sz
);
461 memcpy(itr
, "pre: ", sz_preprefix
);
464 /* include last '\0' */
465 memcpy(itr
, dep
->pre
[0], sz_pre
+ 1);
466 for (p
= itr
; p
< itr
+ sz_pre
; p
++) {
476 memcpy(itr
, "post: ", sz_postprefix
);
477 itr
+= sz_postprefix
;
479 /* include last '\0' */
480 memcpy(itr
, dep
->post
[0], sz_post
+ 1);
481 for (p
= itr
; p
< itr
+ sz_post
; p
++) {
493 static void kmod_config_free_softdep(struct kmod_config
*config
,
497 config
->softdeps
= kmod_list_remove(l
);
500 static void kcmdline_parse_result(struct kmod_config
*config
, char *modname
,
501 char *param
, char *value
)
503 if (modname
== NULL
|| param
== NULL
)
506 DBG(config
->ctx
, "%s %s\n", modname
, param
);
508 if (streq(modname
, "modprobe") && !strncmp(param
, "blacklist=", 10)) {
510 char *t
= strsep(&value
, ",");
514 kmod_config_add_blacklist(config
, t
);
517 kmod_config_add_options(config
,
518 underscores(config
->ctx
, modname
), param
);
522 static int kmod_config_parse_kcmdline(struct kmod_config
*config
)
524 char buf
[KCMD_LINE_SIZE
];
526 char *p
, *modname
, *param
= NULL
, *value
= NULL
, is_module
= 1;
528 fd
= open("/proc/cmdline", O_RDONLY
|O_CLOEXEC
);
531 DBG(config
->ctx
, "could not open '/proc/cmdline' for reading: %m\n");
535 err
= read_str_safe(fd
, buf
, sizeof(buf
));
538 ERR(config
->ctx
, "could not read from '/proc/cmdline': %s\n",
543 for (p
= buf
, modname
= buf
; *p
!= '\0' && *p
!= '\n'; p
++) {
548 kcmdline_parse_result(config
, modname
, param
, value
);
549 param
= value
= NULL
;
570 kcmdline_parse_result(config
, modname
, param
, value
);
576 * Take an fd and own it. It will be closed on return. filename is used only
579 static int kmod_config_parse(struct kmod_config
*config
, int fd
,
580 const char *filename
)
582 struct kmod_ctx
*ctx
= config
->ctx
;
585 unsigned int linenum
= 0;
588 fp
= fdopen(fd
, "r");
591 ERR(config
->ctx
, "fd %d: %m\n", fd
);
596 while ((line
= getline_wrapped(fp
, &linenum
)) != NULL
) {
599 if (line
[0] == '\0' || line
[0] == '#')
602 cmd
= strtok_r(line
, "\t ", &saveptr
);
606 if (streq(cmd
, "alias")) {
607 char *alias
= strtok_r(NULL
, "\t ", &saveptr
);
608 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
610 if (alias
== NULL
|| modname
== NULL
)
613 kmod_config_add_alias(config
,
614 underscores(ctx
, alias
),
615 underscores(ctx
, modname
));
616 } else if (streq(cmd
, "blacklist")) {
617 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
622 kmod_config_add_blacklist(config
,
623 underscores(ctx
, modname
));
624 } else if (streq(cmd
, "options")) {
625 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
626 char *options
= strtok_r(NULL
, "\0", &saveptr
);
628 if (modname
== NULL
|| options
== NULL
)
631 kmod_config_add_options(config
,
632 underscores(ctx
, modname
),
634 } else if (streq(cmd
, "install")) {
635 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
636 char *installcmd
= strtok_r(NULL
, "\0", &saveptr
);
638 if (modname
== NULL
|| installcmd
== NULL
)
641 kmod_config_add_command(config
,
642 underscores(ctx
, modname
),
644 cmd
, &config
->install_commands
);
645 } else if (streq(cmd
, "remove")) {
646 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
647 char *removecmd
= strtok_r(NULL
, "\0", &saveptr
);
649 if (modname
== NULL
|| removecmd
== NULL
)
652 kmod_config_add_command(config
,
653 underscores(ctx
, modname
),
655 cmd
, &config
->remove_commands
);
656 } else if (streq(cmd
, "softdep")) {
657 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
658 char *softdeps
= strtok_r(NULL
, "\0", &saveptr
);
660 if (modname
== NULL
|| softdeps
== NULL
)
663 kmod_config_add_softdep(config
,
664 underscores(ctx
, modname
),
666 } else if (streq(cmd
, "include")
667 || streq(cmd
, "config")) {
668 ERR(ctx
, "%s: command %s is deprecated and not parsed anymore\n",
672 ERR(ctx
, "%s line %u: ignoring bad line starting with '%s'\n",
673 filename
, linenum
, cmd
);
685 void kmod_config_free(struct kmod_config
*config
)
687 while (config
->aliases
)
688 kmod_config_free_alias(config
, config
->aliases
);
690 while (config
->blacklists
)
691 kmod_config_free_blacklist(config
, config
->blacklists
);
693 while (config
->options
)
694 kmod_config_free_options(config
, config
->options
);
696 while (config
->install_commands
) {
697 kmod_config_free_command(config
, config
->install_commands
,
698 &config
->install_commands
);
701 while (config
->remove_commands
) {
702 kmod_config_free_command(config
, config
->remove_commands
,
703 &config
->remove_commands
);
706 while (config
->softdeps
)
707 kmod_config_free_softdep(config
, config
->softdeps
);
709 for (; config
->paths
!= NULL
;
710 config
->paths
= kmod_list_remove(config
->paths
))
711 free(config
->paths
->data
);
716 static bool conf_files_filter_out(struct kmod_ctx
*ctx
, DIR *d
,
717 const char *path
, const char *fn
)
719 size_t len
= strlen(fn
);
725 if (len
< 6 || (!streq(&fn
[len
- 5], ".conf")
726 && !streq(&fn
[len
- 6], ".alias")))
729 fstatat(dirfd(d
), fn
, &st
, 0);
731 if (S_ISDIR(st
.st_mode
)) {
732 ERR(ctx
, "Directories inside directories are not supported: "
733 "%s/%s\n", path
, fn
);
746 static int conf_files_insert_sorted(struct kmod_ctx
*ctx
,
747 struct kmod_list
**list
,
748 const char *path
, const char *name
)
750 struct kmod_list
*lpos
, *tmp
;
751 struct conf_file
*cf
;
754 bool is_single
= false;
757 name
= basename(path
);
761 kmod_list_foreach(lpos
, *list
) {
764 if ((cmp
= strcmp(name
, cf
->name
)) <= 0)
769 DBG(ctx
, "Ignoring duplicate config file: %s/%s\n", path
,
774 namelen
= strlen(name
);
775 cf
= malloc(sizeof(*cf
) + namelen
+ 1);
779 memcpy(cf
->name
, name
, namelen
+ 1);
781 cf
->is_single
= is_single
;
784 tmp
= kmod_list_append(*list
, cf
);
785 else if (lpos
== *list
)
786 tmp
= kmod_list_prepend(*list
, cf
);
788 tmp
= kmod_list_insert_before(lpos
, cf
);
795 if (lpos
== NULL
|| lpos
== *list
)
802 * Insert configuration files in @list, ignoring duplicates
804 static int conf_files_list(struct kmod_ctx
*ctx
, struct kmod_list
**list
,
806 unsigned long long *path_stamp
)
813 if (stat(path
, &st
) != 0) {
815 DBG(ctx
, "could not stat '%s': %m\n", path
);
819 *path_stamp
= stat_mstamp(&st
);
821 if (!S_ISDIR(st
.st_mode
)) {
822 conf_files_insert_sorted(ctx
, list
, path
, NULL
);
828 ERR(ctx
, "opendir(%s): %m\n", path
);
832 for (dent
= readdir(d
); dent
!= NULL
; dent
= readdir(d
)) {
833 if (conf_files_filter_out(ctx
, d
, path
, dent
->d_name
))
836 conf_files_insert_sorted(ctx
, list
, path
, dent
->d_name
);
843 int kmod_config_new(struct kmod_ctx
*ctx
, struct kmod_config
**p_config
,
844 const char * const *config_paths
)
846 struct kmod_config
*config
;
847 struct kmod_list
*list
= NULL
;
848 struct kmod_list
*path_list
= NULL
;
851 conf_files_insert_sorted(ctx
, &list
, kmod_get_dirname(ctx
), "modules.softdep");
853 for (i
= 0; config_paths
[i
] != NULL
; i
++) {
854 const char *path
= config_paths
[i
];
855 unsigned long long path_stamp
= 0;
857 struct kmod_list
*tmp
;
858 struct kmod_config_path
*cf
;
860 if (conf_files_list(ctx
, &list
, path
, &path_stamp
) < 0)
863 pathlen
= strlen(path
) + 1;
864 cf
= malloc(sizeof(*cf
) + pathlen
);
868 cf
->stamp
= path_stamp
;
869 memcpy(cf
->path
, path
, pathlen
);
871 tmp
= kmod_list_append(path_list
, cf
);
877 *p_config
= config
= calloc(1, sizeof(struct kmod_config
));
881 config
->paths
= path_list
;
884 for (; list
!= NULL
; list
= kmod_list_remove(list
)) {
886 struct conf_file
*cf
= list
->data
;
890 strcpy(fn
, cf
->path
);
892 snprintf(fn
, sizeof(fn
),"%s/%s", cf
->path
,
895 fd
= open(fn
, O_RDONLY
|O_CLOEXEC
);
896 DBG(ctx
, "parsing file '%s' fd=%d\n", fn
, fd
);
899 kmod_config_parse(config
, fd
, fn
);
904 kmod_config_parse_kcmdline(config
);
909 for (; list
!= NULL
; list
= kmod_list_remove(list
))
912 for (; path_list
!= NULL
; path_list
= kmod_list_remove(path_list
))
913 free(path_list
->data
);
918 /**********************************************************************
919 * struct kmod_config_iter functions
920 **********************************************************************/
923 CONFIG_TYPE_BLACKLIST
= 0,
931 struct kmod_config_iter
{
932 enum config_type type
;
934 const struct kmod_list
*list
;
935 const struct kmod_list
*curr
;
937 const char *(*get_key
)(const struct kmod_list
*l
);
938 const char *(*get_value
)(const struct kmod_list
*l
);
941 static const char *softdep_get_plain_softdep(const struct kmod_list
*l
)
943 char *s
= softdep_to_char(l
->data
);
947 static struct kmod_config_iter
*kmod_config_iter_new(const struct kmod_ctx
* ctx
,
948 enum config_type type
)
950 struct kmod_config_iter
*iter
= calloc(1, sizeof(*iter
));
951 const struct kmod_config
*config
= kmod_get_config(ctx
);
959 case CONFIG_TYPE_BLACKLIST
:
960 iter
->list
= config
->blacklists
;
961 iter
->get_key
= kmod_blacklist_get_modname
;
963 case CONFIG_TYPE_INSTALL
:
964 iter
->list
= config
->install_commands
;
965 iter
->get_key
= kmod_command_get_modname
;
966 iter
->get_value
= kmod_command_get_command
;
968 case CONFIG_TYPE_REMOVE
:
969 iter
->list
= config
->remove_commands
;
970 iter
->get_key
= kmod_command_get_modname
;
971 iter
->get_value
= kmod_command_get_command
;
973 case CONFIG_TYPE_ALIAS
:
974 iter
->list
= config
->aliases
;
975 iter
->get_key
= kmod_alias_get_name
;
976 iter
->get_value
= kmod_alias_get_modname
;
978 case CONFIG_TYPE_OPTION
:
979 iter
->list
= config
->options
;
980 iter
->get_key
= kmod_option_get_modname
;
981 iter
->get_value
= kmod_option_get_options
;
983 case CONFIG_TYPE_SOFTDEP
:
984 iter
->list
= config
->softdeps
;
985 iter
->get_key
= kmod_softdep_get_name
;
986 iter
->get_value
= softdep_get_plain_softdep
;
987 iter
->intermediate
= true;
995 * SECTION:libkmod-config
996 * @short_description: retrieve current libkmod configuration
1000 * kmod_config_get_blacklists:
1001 * @ctx: kmod library context
1003 * Retrieve an iterator to deal with the blacklist maintained inside the
1004 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1005 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1006 * be made to initialize the iterator and check if it's valid.
1008 * Returns: a new iterator over the blacklists or NULL on failure. Free it
1009 * with kmod_config_iter_free_iter().
1011 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_blacklists(const struct kmod_ctx
*ctx
)
1016 return kmod_config_iter_new(ctx
, CONFIG_TYPE_BLACKLIST
);
1020 * kmod_config_get_install_commands:
1021 * @ctx: kmod library context
1023 * Retrieve an iterator to deal with the install commands maintained inside the
1024 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1025 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1026 * be made to initialize the iterator and check if it's valid.
1028 * Returns: a new iterator over the install commands or NULL on failure. Free
1029 * it with kmod_config_iter_free_iter().
1031 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_install_commands(const struct kmod_ctx
*ctx
)
1036 return kmod_config_iter_new(ctx
, CONFIG_TYPE_INSTALL
);
1040 * kmod_config_get_remove_commands:
1041 * @ctx: kmod library context
1043 * Retrieve an iterator to deal with the remove commands maintained inside the
1044 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1045 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1046 * be made to initialize the iterator and check if it's valid.
1048 * Returns: a new iterator over the remove commands or NULL on failure. Free
1049 * it with kmod_config_iter_free_iter().
1051 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_remove_commands(const struct kmod_ctx
*ctx
)
1056 return kmod_config_iter_new(ctx
, CONFIG_TYPE_REMOVE
);
1060 * kmod_config_get_aliases:
1061 * @ctx: kmod library context
1063 * Retrieve an iterator to deal with the aliases maintained inside the
1064 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1065 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1066 * be made to initialize the iterator and check if it's valid.
1068 * Returns: a new iterator over the aliases or NULL on failure. Free it with
1069 * kmod_config_iter_free_iter().
1071 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_aliases(const struct kmod_ctx
*ctx
)
1076 return kmod_config_iter_new(ctx
, CONFIG_TYPE_ALIAS
);
1080 * kmod_config_get_options:
1081 * @ctx: kmod library context
1083 * Retrieve an iterator to deal with the options maintained inside the
1084 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1085 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1086 * be made to initialize the iterator and check if it's valid.
1088 * Returns: a new iterator over the options or NULL on failure. Free it with
1089 * kmod_config_iter_free_iter().
1091 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_options(const struct kmod_ctx
*ctx
)
1096 return kmod_config_iter_new(ctx
, CONFIG_TYPE_OPTION
);
1100 * kmod_config_get_softdeps:
1101 * @ctx: kmod library context
1103 * Retrieve an iterator to deal with the softdeps maintained inside the
1104 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1105 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1106 * be made to initialize the iterator and check if it's valid.
1108 * Returns: a new iterator over the softdeps or NULL on failure. Free it with
1109 * kmod_config_iter_free_iter().
1111 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_softdeps(const struct kmod_ctx
*ctx
)
1116 return kmod_config_iter_new(ctx
, CONFIG_TYPE_SOFTDEP
);
1120 * kmod_config_iter_get_key:
1121 * @iter: iterator over a certain configuration
1123 * When using a new allocated iterator, user must perform a call to
1124 * kmod_config_iter_next() to initialize iterator's position and check if it's
1127 * Returns: the key of the current configuration pointed by @iter.
1129 KMOD_EXPORT
const char *kmod_config_iter_get_key(const struct kmod_config_iter
*iter
)
1131 if (iter
== NULL
|| iter
->curr
== NULL
)
1134 return iter
->get_key(iter
->curr
);
1138 * kmod_config_iter_get_value:
1139 * @iter: iterator over a certain configuration
1141 * When using a new allocated iterator, user must perform a call to
1142 * kmod_config_iter_next() to initialize iterator's position and check if it's
1145 * Returns: the value of the current configuration pointed by @iter.
1147 KMOD_EXPORT
const char *kmod_config_iter_get_value(const struct kmod_config_iter
*iter
)
1151 if (iter
== NULL
|| iter
->curr
== NULL
)
1154 if (iter
->get_value
== NULL
)
1157 if (iter
->intermediate
) {
1158 struct kmod_config_iter
*i
= (struct kmod_config_iter
*)iter
;
1161 s
= i
->data
= (void *) iter
->get_value(iter
->curr
);
1163 s
= iter
->get_value(iter
->curr
);
1169 * kmod_config_iter_next:
1170 * @iter: iterator over a certain configuration
1172 * Make @iter point to the next item of a certain configuration. It's an
1173 * automatically recycling iterator. When it reaches the end, false is
1174 * returned; then if user wants to iterate again, it's sufficient to call this
1175 * function once more.
1177 * Returns: true if next position of @iter is valid or false if its end is
1180 KMOD_EXPORT
bool kmod_config_iter_next(struct kmod_config_iter
*iter
)
1185 if (iter
->curr
== NULL
) {
1186 iter
->curr
= iter
->list
;
1187 return iter
->curr
!= NULL
;
1190 iter
->curr
= kmod_list_next(iter
->list
, iter
->curr
);
1192 return iter
->curr
!= NULL
;
1196 * kmod_config_iter_free_iter:
1197 * @iter: iterator over a certain configuration
1199 * Free resources used by the iterator.
1201 KMOD_EXPORT
void kmod_config_iter_free_iter(struct kmod_config_iter
*iter
)