2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011-2012 ProFUSION embedded systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/types.h>
34 #include "libkmod-private.h"
59 const char *kmod_blacklist_get_modname(const struct kmod_list
*l
)
64 const char *kmod_alias_get_name(const struct kmod_list
*l
) {
65 const struct kmod_alias
*alias
= l
->data
;
69 const char *kmod_alias_get_modname(const struct kmod_list
*l
) {
70 const struct kmod_alias
*alias
= l
->data
;
71 return alias
->modname
;
74 const char *kmod_option_get_options(const struct kmod_list
*l
) {
75 const struct kmod_options
*alias
= l
->data
;
76 return alias
->options
;
79 const char *kmod_option_get_modname(const struct kmod_list
*l
) {
80 const struct kmod_options
*alias
= l
->data
;
81 return alias
->modname
;
84 const char *kmod_command_get_command(const struct kmod_list
*l
) {
85 const struct kmod_command
*alias
= l
->data
;
86 return alias
->command
;
89 const char *kmod_command_get_modname(const struct kmod_list
*l
) {
90 const struct kmod_command
*alias
= l
->data
;
91 return alias
->modname
;
94 const char *kmod_softdep_get_name(const struct kmod_list
*l
) {
95 const struct kmod_softdep
*dep
= l
->data
;
99 const char * const *kmod_softdep_get_pre(const struct kmod_list
*l
, unsigned int *count
) {
100 const struct kmod_softdep
*dep
= l
->data
;
105 const char * const *kmod_softdep_get_post(const struct kmod_list
*l
, unsigned int *count
) {
106 const struct kmod_softdep
*dep
= l
->data
;
107 *count
= dep
->n_post
;
111 static int kmod_config_add_command(struct kmod_config
*config
,
114 const char *command_name
,
115 struct kmod_list
**list
)
117 struct kmod_command
*cmd
;
119 size_t modnamelen
= strlen(modname
) + 1;
120 size_t commandlen
= strlen(command
) + 1;
122 DBG(config
->ctx
, "modname='%s' cmd='%s %s'\n", modname
, command_name
,
125 cmd
= malloc(sizeof(*cmd
) + modnamelen
+ commandlen
);
129 cmd
->command
= sizeof(*cmd
) + modnamelen
+ (char *)cmd
;
130 memcpy(cmd
->modname
, modname
, modnamelen
);
131 memcpy(cmd
->command
, command
, commandlen
);
133 l
= kmod_list_append(*list
, cmd
);
143 ERR(config
->ctx
, "out-of-memory\n");
147 static void kmod_config_free_command(struct kmod_config
*config
,
149 struct kmod_list
**list
)
151 struct kmod_command
*cmd
= l
->data
;
154 *list
= kmod_list_remove(l
);
157 static int kmod_config_add_options(struct kmod_config
*config
,
158 const char *modname
, const char *options
)
160 struct kmod_options
*opt
;
161 struct kmod_list
*list
;
162 size_t modnamelen
= strlen(modname
) + 1;
163 size_t optionslen
= strlen(options
) + 1;
165 DBG(config
->ctx
, "modname='%s' options='%s'\n", modname
, options
);
167 opt
= malloc(sizeof(*opt
) + modnamelen
+ optionslen
);
171 opt
->options
= sizeof(*opt
) + modnamelen
+ (char *)opt
;
173 memcpy(opt
->modname
, modname
, modnamelen
);
174 memcpy(opt
->options
, options
, optionslen
);
175 strchr_replace(opt
->options
, '\t', ' ');
177 list
= kmod_list_append(config
->options
, opt
);
181 config
->options
= list
;
187 ERR(config
->ctx
, "out-of-memory\n");
191 static void kmod_config_free_options(struct kmod_config
*config
,
194 struct kmod_options
*opt
= l
->data
;
198 config
->options
= kmod_list_remove(l
);
201 static int kmod_config_add_alias(struct kmod_config
*config
,
202 const char *name
, const char *modname
)
204 struct kmod_alias
*alias
;
205 struct kmod_list
*list
;
206 size_t namelen
= strlen(name
) + 1, modnamelen
= strlen(modname
) + 1;
208 DBG(config
->ctx
, "name=%s modname=%s\n", name
, modname
);
210 alias
= malloc(sizeof(*alias
) + namelen
+ modnamelen
);
214 alias
->name
= sizeof(*alias
) + modnamelen
+ (char *)alias
;
216 memcpy(alias
->modname
, modname
, modnamelen
);
217 memcpy(alias
->name
, name
, namelen
);
219 list
= kmod_list_append(config
->aliases
, alias
);
223 config
->aliases
= list
;
229 ERR(config
->ctx
, "out-of-memory name=%s modname=%s\n", name
, modname
);
233 static void kmod_config_free_alias(struct kmod_config
*config
,
236 struct kmod_alias
*alias
= l
->data
;
240 config
->aliases
= kmod_list_remove(l
);
243 static int kmod_config_add_blacklist(struct kmod_config
*config
,
247 struct kmod_list
*list
;
249 DBG(config
->ctx
, "modname=%s\n", modname
);
255 list
= kmod_list_append(config
->blacklists
, p
);
258 config
->blacklists
= list
;
264 ERR(config
->ctx
, "out-of-memory modname=%s\n", modname
);
268 static void kmod_config_free_blacklist(struct kmod_config
*config
,
272 config
->blacklists
= kmod_list_remove(l
);
275 static int kmod_config_add_softdep(struct kmod_config
*config
,
279 struct kmod_list
*list
;
280 struct kmod_softdep
*dep
;
283 unsigned int n_pre
= 0, n_post
= 0;
284 size_t modnamelen
= strlen(modname
) + 1;
286 bool was_space
= false;
287 enum { S_NONE
, S_PRE
, S_POST
} mode
= S_NONE
;
289 DBG(config
->ctx
, "modname=%s\n", modname
);
291 /* analyze and count */
292 for (p
= s
= line
; ; s
++) {
312 if (plen
== sizeof("pre:") - 1 &&
313 memcmp(p
, "pre:", sizeof("pre:") - 1) == 0)
315 else if (plen
== sizeof("post:") - 1 &&
316 memcmp(p
, "post:", sizeof("post:") - 1) == 0)
318 else if (*s
!= '\0' || (*s
== '\0' && !was_space
)) {
322 } else if (mode
== S_POST
) {
332 DBG(config
->ctx
, "%u pre, %u post\n", n_pre
, n_post
);
334 dep
= malloc(sizeof(struct kmod_softdep
) + modnamelen
+
335 n_pre
* sizeof(const char *) +
336 n_post
* sizeof(const char *) +
339 ERR(config
->ctx
, "out-of-memory modname=%s\n", modname
);
343 dep
->n_post
= n_post
;
344 dep
->pre
= (const char **)((char *)dep
+ sizeof(struct kmod_softdep
));
345 dep
->post
= dep
->pre
+ n_pre
;
346 dep
->name
= (char *)(dep
->post
+ n_post
);
348 memcpy(dep
->name
, modname
, modnamelen
);
351 itr
= dep
->name
+ modnamelen
;
355 for (p
= s
= line
; ; s
++) {
375 if (plen
== sizeof("pre:") - 1 &&
376 memcmp(p
, "pre:", sizeof("pre:") - 1) == 0)
378 else if (plen
== sizeof("post:") - 1 &&
379 memcmp(p
, "post:", sizeof("post:") - 1) == 0)
381 else if (*s
!= '\0' || (*s
== '\0' && !was_space
)) {
383 dep
->pre
[n_pre
] = itr
;
384 memcpy(itr
, p
, plen
);
388 } else if (mode
== S_POST
) {
389 dep
->post
[n_post
] = itr
;
390 memcpy(itr
, p
, plen
);
401 list
= kmod_list_append(config
->softdeps
, dep
);
406 config
->softdeps
= list
;
411 static char *softdep_to_char(struct kmod_softdep
*dep
) {
412 const size_t sz_preprefix
= sizeof("pre: ") - 1;
413 const size_t sz_postprefix
= sizeof("post: ") - 1;
414 size_t sz
= 1; /* at least '\0' */
415 size_t sz_pre
, sz_post
;
416 const char *start
, *end
;
420 * Rely on the fact that dep->pre[] and dep->post[] are strv's that
421 * point to a contiguous buffer
423 if (dep
->n_pre
> 0) {
425 end
= dep
->pre
[dep
->n_pre
- 1]
426 + strlen(dep
->pre
[dep
->n_pre
- 1]);
427 sz_pre
= end
- start
;
428 sz
+= sz_pre
+ sz_preprefix
;
432 if (dep
->n_post
> 0) {
433 start
= dep
->post
[0];
434 end
= dep
->post
[dep
->n_post
- 1]
435 + strlen(dep
->post
[dep
->n_post
- 1]);
436 sz_post
= end
- start
;
437 sz
+= sz_post
+ sz_postprefix
;
441 itr
= s
= malloc(sz
);
448 memcpy(itr
, "pre: ", sz_preprefix
);
451 /* include last '\0' */
452 memcpy(itr
, dep
->pre
[0], sz_pre
+ 1);
453 for (p
= itr
; p
< itr
+ sz_pre
; p
++) {
463 memcpy(itr
, "post: ", sz_postprefix
);
464 itr
+= sz_postprefix
;
466 /* include last '\0' */
467 memcpy(itr
, dep
->post
[0], sz_post
+ 1);
468 for (p
= itr
; p
< itr
+ sz_post
; p
++) {
480 static void kmod_config_free_softdep(struct kmod_config
*config
,
484 config
->softdeps
= kmod_list_remove(l
);
487 static void kcmdline_parse_result(struct kmod_config
*config
, char *modname
,
488 char *param
, char *value
)
490 if (modname
== NULL
|| param
== NULL
|| value
== NULL
)
493 DBG(config
->ctx
, "%s %s\n", modname
, param
);
495 if (streq(modname
, "modprobe") && !strncmp(param
, "blacklist=", 10)) {
497 char *t
= strsep(&value
, ",");
501 kmod_config_add_blacklist(config
, t
);
504 kmod_config_add_options(config
,
505 underscores(config
->ctx
, modname
), param
);
509 static int kmod_config_parse_kcmdline(struct kmod_config
*config
)
511 char buf
[KCMD_LINE_SIZE
];
513 char *p
, *modname
, *param
= NULL
, *value
= NULL
;
515 fd
= open("/proc/cmdline", O_RDONLY
|O_CLOEXEC
);
518 DBG(config
->ctx
, "could not open '/proc/cmdline' for reading: %m\n");
522 err
= read_str_safe(fd
, buf
, sizeof(buf
));
525 ERR(config
->ctx
, "could not read from '/proc/cmdline': %s\n",
530 for (p
= buf
, modname
= buf
; *p
!= '\0' && *p
!= '\n'; p
++) {
534 kcmdline_parse_result(config
, modname
, param
, value
);
535 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", fd
);
576 while ((line
= getline_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 (alias
== NULL
|| modname
== NULL
)
593 kmod_config_add_alias(config
,
594 underscores(ctx
, alias
),
595 underscores(ctx
, modname
));
596 } else if (streq(cmd
, "blacklist")) {
597 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
602 kmod_config_add_blacklist(config
,
603 underscores(ctx
, modname
));
604 } else if (streq(cmd
, "options")) {
605 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
606 char *options
= strtok_r(NULL
, "\0", &saveptr
);
608 if (modname
== NULL
|| options
== NULL
)
611 kmod_config_add_options(config
,
612 underscores(ctx
, modname
),
614 } else if (streq(cmd
, "install")) {
615 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
616 char *installcmd
= strtok_r(NULL
, "\0", &saveptr
);
618 if (modname
== NULL
|| installcmd
== NULL
)
621 kmod_config_add_command(config
,
622 underscores(ctx
, modname
),
624 cmd
, &config
->install_commands
);
625 } else if (streq(cmd
, "remove")) {
626 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
627 char *removecmd
= strtok_r(NULL
, "\0", &saveptr
);
629 if (modname
== NULL
|| removecmd
== NULL
)
632 kmod_config_add_command(config
,
633 underscores(ctx
, modname
),
635 cmd
, &config
->remove_commands
);
636 } else if (streq(cmd
, "softdep")) {
637 char *modname
= strtok_r(NULL
, "\t ", &saveptr
);
638 char *softdeps
= strtok_r(NULL
, "\0", &saveptr
);
640 if (modname
== NULL
|| softdeps
== NULL
)
643 kmod_config_add_softdep(config
,
644 underscores(ctx
, modname
),
646 } else if (streq(cmd
, "include")
647 || streq(cmd
, "config")) {
648 ERR(ctx
, "%s: command %s is deprecated and not parsed anymore\n",
652 ERR(ctx
, "%s line %u: ignoring bad line starting with '%s'\n",
653 filename
, linenum
, cmd
);
665 void kmod_config_free(struct kmod_config
*config
)
667 while (config
->aliases
)
668 kmod_config_free_alias(config
, config
->aliases
);
670 while (config
->blacklists
)
671 kmod_config_free_blacklist(config
, config
->blacklists
);
673 while (config
->options
)
674 kmod_config_free_options(config
, config
->options
);
676 while (config
->install_commands
) {
677 kmod_config_free_command(config
, config
->install_commands
,
678 &config
->install_commands
);
681 while (config
->remove_commands
) {
682 kmod_config_free_command(config
, config
->remove_commands
,
683 &config
->remove_commands
);
686 while (config
->softdeps
)
687 kmod_config_free_softdep(config
, config
->softdeps
);
689 for (; config
->paths
!= NULL
;
690 config
->paths
= kmod_list_remove(config
->paths
))
691 free(config
->paths
->data
);
696 static bool conf_files_filter_out(struct kmod_ctx
*ctx
, DIR *d
,
697 const char *path
, const char *fn
)
699 size_t len
= strlen(fn
);
705 if (len
< 6 || (!streq(&fn
[len
- 5], ".conf")
706 && !streq(&fn
[len
- 6], ".alias")))
709 fstatat(dirfd(d
), fn
, &st
, 0);
711 if (S_ISDIR(st
.st_mode
)) {
712 ERR(ctx
, "Directories inside directories are not supported: "
713 "%s/%s\n", path
, fn
);
726 static int conf_files_insert_sorted(struct kmod_ctx
*ctx
,
727 struct kmod_list
**list
,
728 const char *path
, const char *name
)
730 struct kmod_list
*lpos
, *tmp
;
731 struct conf_file
*cf
;
734 bool is_single
= false;
737 name
= basename(path
);
741 kmod_list_foreach(lpos
, *list
) {
744 if ((cmp
= strcmp(name
, cf
->name
)) <= 0)
749 DBG(ctx
, "Ignoring duplicate config file: %s/%s\n", path
,
754 namelen
= strlen(name
);
755 cf
= malloc(sizeof(*cf
) + namelen
+ 1);
759 memcpy(cf
->name
, name
, namelen
+ 1);
761 cf
->is_single
= is_single
;
764 tmp
= kmod_list_append(*list
, cf
);
765 else if (lpos
== *list
)
766 tmp
= kmod_list_prepend(*list
, cf
);
768 tmp
= kmod_list_insert_before(lpos
, cf
);
775 if (lpos
== NULL
|| lpos
== *list
)
782 * Insert configuration files in @list, ignoring duplicates
784 static int conf_files_list(struct kmod_ctx
*ctx
, struct kmod_list
**list
,
786 unsigned long long *path_stamp
)
792 if (stat(path
, &st
) != 0) {
794 DBG(ctx
, "could not stat '%s': %m\n", path
);
798 *path_stamp
= ts_usec(&st
.st_mtim
);
800 if (S_ISREG(st
.st_mode
)) {
801 conf_files_insert_sorted(ctx
, list
, path
, NULL
);
803 } if (!S_ISDIR(st
.st_mode
)) {
804 ERR(ctx
, "unsupported file mode %s: %#x\n",
816 struct dirent ent
, *entp
;
818 err
= readdir_r(d
, &ent
, &entp
);
820 ERR(ctx
, "reading entry %s\n", strerror(-err
));
827 if (conf_files_filter_out(ctx
, d
, path
, entp
->d_name
))
830 conf_files_insert_sorted(ctx
, list
, path
, entp
->d_name
);
841 int kmod_config_new(struct kmod_ctx
*ctx
, struct kmod_config
**p_config
,
842 const char * const *config_paths
)
844 struct kmod_config
*config
;
845 struct kmod_list
*list
= NULL
;
846 struct kmod_list
*path_list
= NULL
;
849 for (i
= 0; config_paths
[i
] != NULL
; i
++) {
850 const char *path
= config_paths
[i
];
851 unsigned long long path_stamp
= 0;
853 struct kmod_list
*tmp
;
854 struct kmod_config_path
*cf
;
856 if (conf_files_list(ctx
, &list
, path
, &path_stamp
) < 0)
859 pathlen
= strlen(path
) + 1;
860 cf
= malloc(sizeof(*cf
) + pathlen
);
864 cf
->stamp
= path_stamp
;
865 memcpy(cf
->path
, path
, pathlen
);
867 tmp
= kmod_list_append(path_list
, cf
);
873 *p_config
= config
= calloc(1, sizeof(struct kmod_config
));
877 config
->paths
= path_list
;
880 for (; list
!= NULL
; list
= kmod_list_remove(list
)) {
882 struct conf_file
*cf
= list
->data
;
886 strcpy(fn
, cf
->path
);
888 snprintf(fn
, sizeof(fn
),"%s/%s", cf
->path
,
891 fd
= open(fn
, O_RDONLY
|O_CLOEXEC
);
892 DBG(ctx
, "parsing file '%s' fd=%d\n", fn
, fd
);
895 kmod_config_parse(config
, fd
, fn
);
900 kmod_config_parse_kcmdline(config
);
905 for (; list
!= NULL
; list
= kmod_list_remove(list
))
908 for (; path_list
!= NULL
; path_list
= kmod_list_remove(path_list
))
909 free(path_list
->data
);
914 /**********************************************************************
915 * struct kmod_config_iter functions
916 **********************************************************************/
919 CONFIG_TYPE_BLACKLIST
= 0,
927 struct kmod_config_iter
{
928 enum config_type type
;
930 const struct kmod_list
*list
;
931 const struct kmod_list
*curr
;
933 const char *(*get_key
)(const struct kmod_list
*l
);
934 const char *(*get_value
)(const struct kmod_list
*l
);
937 static const char *softdep_get_plain_softdep(const struct kmod_list
*l
)
939 char *s
= softdep_to_char(l
->data
);
943 static struct kmod_config_iter
*kmod_config_iter_new(const struct kmod_ctx
* ctx
,
944 enum config_type type
)
946 struct kmod_config_iter
*iter
= calloc(1, sizeof(*iter
));
954 case CONFIG_TYPE_BLACKLIST
:
955 iter
->list
= kmod_get_blacklists(ctx
);
956 iter
->get_key
= kmod_blacklist_get_modname
;
958 case CONFIG_TYPE_INSTALL
:
959 iter
->list
= kmod_get_install_commands(ctx
);
960 iter
->get_key
= kmod_command_get_modname
;
961 iter
->get_value
= kmod_command_get_command
;
963 case CONFIG_TYPE_REMOVE
:
964 iter
->list
= kmod_get_remove_commands(ctx
);
965 iter
->get_key
= kmod_command_get_modname
;
966 iter
->get_value
= kmod_command_get_command
;
968 case CONFIG_TYPE_ALIAS
:
969 iter
->list
= kmod_get_aliases(ctx
);
970 iter
->get_key
= kmod_alias_get_name
;
971 iter
->get_value
= kmod_alias_get_modname
;
973 case CONFIG_TYPE_OPTION
:
974 iter
->list
= kmod_get_options(ctx
);
975 iter
->get_key
= kmod_option_get_modname
;
976 iter
->get_value
= kmod_option_get_options
;
978 case CONFIG_TYPE_SOFTDEP
:
979 iter
->list
= kmod_get_softdeps(ctx
);
980 iter
->get_key
= kmod_softdep_get_name
;
981 iter
->get_value
= softdep_get_plain_softdep
;
982 iter
->intermediate
= true;
990 * SECTION:libkmod-config
991 * @short_description: retrieve current libkmod configuration
995 * kmod_config_get_blacklists:
996 * @ctx: kmod library context
998 * Retrieve an iterator to deal with the blacklist maintained inside the
999 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1000 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1001 * be made to initialize the iterator and check if it's valid.
1003 * Returns: a new iterator over the blacklists or %NULL on failure. Free it
1004 * with kmod_config_iter_free_iter().
1006 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_blacklists(const struct kmod_ctx
*ctx
)
1011 return kmod_config_iter_new(ctx
, CONFIG_TYPE_BLACKLIST
);
1015 * kmod_config_get_install_commands:
1016 * @ctx: kmod library context
1018 * Retrieve an iterator to deal with the install commands maintained inside the
1019 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1020 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1021 * be made to initialize the iterator and check if it's valid.
1023 * Returns: a new iterator over the install commands or %NULL on failure. Free
1024 * it with kmod_config_iter_free_iter().
1026 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_install_commands(const struct kmod_ctx
*ctx
)
1031 return kmod_config_iter_new(ctx
, CONFIG_TYPE_INSTALL
);
1035 * kmod_config_get_remove_commands:
1036 * @ctx: kmod library context
1038 * Retrieve an iterator to deal with the remove commands maintained inside the
1039 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1040 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1041 * be made to initialize the iterator and check if it's valid.
1043 * Returns: a new iterator over the remove commands or %NULL on failure. Free
1044 * it with kmod_config_iter_free_iter().
1046 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_remove_commands(const struct kmod_ctx
*ctx
)
1051 return kmod_config_iter_new(ctx
, CONFIG_TYPE_REMOVE
);
1055 * kmod_config_get_aliases:
1056 * @ctx: kmod library context
1058 * Retrieve an iterator to deal with the aliases maintained inside the
1059 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1060 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1061 * be made to initialize the iterator and check if it's valid.
1063 * Returns: a new iterator over the aliases or %NULL on failure. Free it with
1064 * kmod_config_iter_free_iter().
1066 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_aliases(const struct kmod_ctx
*ctx
)
1071 return kmod_config_iter_new(ctx
, CONFIG_TYPE_ALIAS
);
1075 * kmod_config_get_options:
1076 * @ctx: kmod library context
1078 * Retrieve an iterator to deal with the options maintained inside the
1079 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1080 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1081 * be made to initialize the iterator and check if it's valid.
1083 * Returns: a new iterator over the options or %NULL on failure. Free it with
1084 * kmod_config_iter_free_iter().
1086 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_options(const struct kmod_ctx
*ctx
)
1091 return kmod_config_iter_new(ctx
, CONFIG_TYPE_OPTION
);
1095 * kmod_config_get_softdeps:
1096 * @ctx: kmod library context
1098 * Retrieve an iterator to deal with the softdeps maintained inside the
1099 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1100 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1101 * be made to initialize the iterator and check if it's valid.
1103 * Returns: a new iterator over the softdeps or %NULL on failure. Free it with
1104 * kmod_config_iter_free_iter().
1106 KMOD_EXPORT
struct kmod_config_iter
*kmod_config_get_softdeps(const struct kmod_ctx
*ctx
)
1111 return kmod_config_iter_new(ctx
, CONFIG_TYPE_SOFTDEP
);
1115 * kmod_config_iter_get_key:
1116 * @iter: iterator over a certain configuration
1118 * When using a new allocated iterator, user must perform a call to
1119 * kmod_config_iter_next() to initialize iterator's position and check if it's
1122 * Returns: the key of the current configuration pointed by @iter.
1124 KMOD_EXPORT
const char *kmod_config_iter_get_key(const struct kmod_config_iter
*iter
)
1126 if (iter
== NULL
|| iter
->curr
== NULL
)
1129 return iter
->get_key(iter
->curr
);
1133 * kmod_config_iter_get_value:
1134 * @iter: iterator over a certain configuration
1136 * When using a new allocated iterator, user must perform a call to
1137 * kmod_config_iter_next() to initialize iterator's position and check if it's
1140 * Returns: the value of the current configuration pointed by @iter.
1142 KMOD_EXPORT
const char *kmod_config_iter_get_value(const struct kmod_config_iter
*iter
)
1146 if (iter
== NULL
|| iter
->curr
== NULL
)
1149 if (iter
->get_value
== NULL
)
1152 if (iter
->intermediate
) {
1153 struct kmod_config_iter
*i
= (struct kmod_config_iter
*)iter
;
1156 s
= i
->data
= (void *) iter
->get_value(iter
->curr
);
1158 s
= iter
->get_value(iter
->curr
);
1164 * kmod_config_iter_next:
1165 * @iter: iterator over a certain configuration
1167 * Make @iter point to the next item of a certain configuration. It's an
1168 * automatically recycling iterator. When it reaches the end, false is
1169 * returned; then if user wants to iterate again, it's sufficient to call this
1170 * function once more.
1172 * Returns: true if next position of @iter is valid or false if its end is
1175 KMOD_EXPORT
bool kmod_config_iter_next(struct kmod_config_iter
*iter
)
1180 if (iter
->curr
== NULL
) {
1181 iter
->curr
= iter
->list
;
1182 return iter
->curr
!= NULL
;
1185 iter
->curr
= kmod_list_next(iter
->list
, iter
->curr
);
1187 return iter
->curr
!= NULL
;
1191 * kmod_config_iter_free_iter:
1192 * @iter: iterator over a certain configuration
1194 * Free resources used by the iterator.
1196 KMOD_EXPORT
void kmod_config_iter_free_iter(struct kmod_config_iter
*iter
)