2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011 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
34 #include <sys/types.h>
39 #include "libkmod-private.h"
44 * Opaque object representing a module.
50 struct kmod_list
*dep
;
52 char *install_commands
;
53 char *remove_commands
;
54 char *alias
; /* only set if this module was created from an alias */
60 bool install_commands
: 1;
61 bool remove_commands
: 1;
65 inline char *modname_normalize(const char *modname
, char buf
[NAME_MAX
],
70 for (s
= 0; s
< NAME_MAX
- 1; s
++) {
71 const char c
= modname
[s
];
74 else if (c
== '\0' || c
== '.')
88 static char *path_to_modname(const char *path
, char buf
[NAME_MAX
], size_t *len
)
92 modname
= basename(path
);
93 if (modname
== NULL
|| modname
[0] == '\0')
96 return modname_normalize(modname
, buf
, len
);
99 static inline const char *path_join(const char *path
, size_t prefixlen
,
107 pathlen
= strlen(path
);
108 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
111 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
115 int kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
117 struct kmod_ctx
*ctx
= mod
->ctx
;
118 struct kmod_list
*list
= NULL
;
127 assert(mod
->dep
== NULL
);
128 mod
->init
.dep
= true;
130 p
= strchr(line
, ':');
135 dirname
= kmod_get_dirname(mod
->ctx
);
136 dirnamelen
= strlen(dirname
);
137 if (dirnamelen
+ 2 >= PATH_MAX
)
140 memcpy(buf
, dirname
, dirnamelen
);
141 buf
[dirnamelen
] = '/';
143 buf
[dirnamelen
] = '\0';
145 if (mod
->path
== NULL
) {
146 const char *str
= path_join(line
, dirnamelen
, buf
);
149 mod
->path
= strdup(str
);
150 if (mod
->path
== NULL
)
155 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
156 p
= strtok_r(NULL
, " \t", &saveptr
)) {
157 struct kmod_module
*depmod
;
160 path
= path_join(p
, dirnamelen
, buf
);
162 ERR(ctx
, "could not join path '%s' and '%s'.\n",
167 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
169 ERR(ctx
, "ctx=%p path=%s error=%s\n",
170 ctx
, path
, strerror(-err
));
174 DBG(ctx
, "add dep: %s\n", path
);
176 list
= kmod_list_append(list
, depmod
);
180 DBG(ctx
, "%d dependencies for %s\n", n
, mod
->name
);
187 kmod_module_unref_list(list
);
188 mod
->init
.dep
= false;
193 * kmod_module_new_from_name:
194 * @ctx: kmod library context
195 * @name: name of the module
196 * @mod: where to save the created struct kmod_module
198 * Create a new struct kmod_module using the module name. @name can not be an
199 * alias, file name or anything else; it must be a module name. There's no
200 * check if the module does exists in the system.
202 * This function is also used internally by many others that return a new
203 * struct kmod_module or a new list of modules.
205 * The initial refcount is 1, and needs to be decremented to release the
206 * resources of the kmod_module. Since libkmod keeps track of all
207 * kmod_modules created, they are all released upon @ctx destruction too. Do
208 * not unref @ctx before all the desired operations with the returned
209 * kmod_module are done.
211 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
212 * module name or if memory allocation failed.
214 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
216 struct kmod_module
**mod
)
218 struct kmod_module
*m
;
220 char name_norm
[NAME_MAX
];
222 if (ctx
== NULL
|| name
== NULL
)
225 alias_normalize(name
, name_norm
, &namelen
);
227 m
= kmod_pool_get_module(ctx
, name_norm
);
229 *mod
= kmod_module_ref(m
);
233 m
= calloc(1, sizeof(*m
) + namelen
+ 1);
239 m
->ctx
= kmod_ref(ctx
);
240 m
->name
= (char *)m
+ sizeof(*m
);
241 memcpy(m
->name
, name_norm
, namelen
+ 1);
244 kmod_pool_add_module(ctx
, m
);
251 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
252 const char *name
, struct kmod_module
**mod
)
255 struct kmod_module
*m
;
257 err
= kmod_module_new_from_name(ctx
, alias
, mod
);
263 /* if module did not came from pool */
264 if (m
->alias
== NULL
) {
266 m
->name
= strdup(name
);
274 ERR(ctx
, "out of memory\n");
275 kmod_module_unref(m
);
281 * kmod_module_new_from_path:
282 * @ctx: kmod library context
283 * @path: path where to find the given module
284 * @mod: where to save the created struct kmod_module
286 * Create a new struct kmod_module using the module path. @path must be an
287 * existent file with in the filesystem and must be accessible to libkmod.
289 * The initial refcount is 1, and needs to be decremented to release the
290 * resources of the kmod_module. Since libkmod keeps track of all
291 * kmod_modules created, they are all released upon @ctx destruction too. Do
292 * not unref @ctx before all the desired operations with the returned
293 * kmod_module are done.
295 * If @path is relative, it's treated as relative to the current working
296 * directory. Otherwise, give an absolute path.
298 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
299 * it's not a valid file for a kmod_module or if memory allocation failed.
301 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
303 struct kmod_module
**mod
)
305 struct kmod_module
*m
;
312 if (ctx
== NULL
|| path
== NULL
)
315 abspath
= path_make_absolute_cwd(path
);
319 err
= stat(abspath
, &st
);
325 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
330 m
= kmod_pool_get_module(ctx
, name
);
334 else if (streq(m
->path
, abspath
))
337 ERR(ctx
, "kmod_module '%s' already exists with different path\n",
343 *mod
= kmod_module_ref(m
);
347 m
= calloc(1, sizeof(*m
) + namelen
+ 1);
351 m
->ctx
= kmod_ref(ctx
);
352 m
->name
= (char *)m
+ sizeof(*m
);
353 memcpy(m
->name
, name
, namelen
);
357 kmod_pool_add_module(ctx
, m
);
368 * Drop a reference of the kmod module. If the refcount reaches zero, its
369 * resources are released.
371 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
372 * returns the passed @mod with its refcount decremented.
374 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
379 if (--mod
->refcount
> 0)
382 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
384 kmod_module_unref_list(mod
->dep
);
385 kmod_unref(mod
->ctx
);
387 free(mod
->install_commands
);
388 free(mod
->remove_commands
);
390 if (mod
->alias
!= NULL
)
400 * Take a reference of the kmod module, incrementing its refcount.
402 * Returns: the passed @module with its refcount incremented.
404 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
414 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
418 if (*(_list) != NULL) \
423 * kmod_module_new_from_lookup:
424 * @ctx: kmod library context
425 * @given_alias: alias to look for
426 * @list: an empty list where to save the list of modules matching
429 * Create a new list of kmod modules using an alias or module name and lookup
430 * libkmod's configuration files and indexes in order to find the module.
431 * Once it's found in one of the places, it stops searching and create the
432 * list of modules that is saved in @list.
434 * The search order is: 1. aliases in configuration file; 2. module names in
435 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
436 * in modules.alias index.
438 * The initial refcount is 1, and needs to be decremented to release the
439 * resources of the kmod_module. The returned @list must be released by
440 * calling kmod_module_unref_list(). Since libkmod keeps track of all
441 * kmod_modules created, they are all released upon @ctx destruction too. Do
442 * not unref @ctx before all the desired operations with the returned list are
445 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
446 * methods failed, which is basically due to memory allocation fail. If module
447 * is not found, it still returns 0, but @list is an empty list.
449 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
450 const char *given_alias
,
451 struct kmod_list
**list
)
454 char alias
[NAME_MAX
];
456 if (ctx
== NULL
|| given_alias
== NULL
)
459 if (list
== NULL
|| *list
!= NULL
) {
460 ERR(ctx
, "An empty list is needed to create lookup\n");
464 if (alias_normalize(given_alias
, alias
, NULL
) < 0)
467 /* Aliases from config file override all the others */
468 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
469 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
471 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
472 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
474 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
475 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
477 // TODO: add lookup for install commands here.
479 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
480 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
486 kmod_module_unref_list(*list
);
490 #undef CHECK_ERR_AND_FINISH
494 * @list: list of kmod modules
496 * Drop a reference of each kmod module in @list and releases the resources
497 * taken by the list itself.
499 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
500 * returns the passed @mod with its refcount decremented.
502 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
504 for (; list
!= NULL
; list
= kmod_list_remove(list
))
505 kmod_module_unref(list
->data
);
514 * Search the modules.dep index to find the dependencies of the given @mod.
515 * The result is cached in @mod, so subsequent calls to this function will
516 * return the already searched list of modules.
518 * Returns: NULL on failure or if there are any dependencies. Otherwise it
519 * returns a list of kmod modules that can be released by calling
520 * kmod_module_unref_list().
522 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
524 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
529 if (!mod
->init
.dep
) {
531 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
536 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
543 kmod_list_foreach(l
, mod
->dep
) {
544 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
546 kmod_module_unref(l
->data
);
556 ERR(mod
->ctx
, "out of memory\n");
557 kmod_module_unref_list(list_new
);
562 * kmod_module_get_module:
563 * @entry: an entry in a list of kmod modules.
565 * Get the kmod module of this @entry in the list, increasing its refcount.
566 * After it's used, unref it. Since the refcount is incremented upon return,
567 * you still have to call kmod_module_unref_list() to release the list of kmod
570 * Returns: NULL on failure or the kmod_module contained in this list entry
571 * with its refcount incremented.
573 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
578 return kmod_module_ref(entry
->data
);
582 * kmod_module_get_size:
585 * Get the size of this kmod module as returned by Linux kernel. It reads the
586 * file /proc/modules to search for this module and get its size.
588 * Returns: the size of this kmod module.
590 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
592 // FIXME TODO: this should be available from /sys/module/foo
601 fp
= fopen("/proc/modules", "r");
605 "could not open /proc/modules: %s\n", strerror(errno
));
609 while (fgets(line
, sizeof(line
), fp
)) {
610 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
614 if (tok
== NULL
|| !streq(tok
, mod
->name
))
617 tok
= strtok_r(NULL
, " \t", &saveptr
);
620 "invalid line format at /proc/modules:%d\n", lineno
);
624 value
= strtol(tok
, &endptr
, 10);
625 if (endptr
== tok
|| *endptr
!= '\0') {
627 "invalid line format at /proc/modules:%d\n", lineno
);
639 * kmod_module_get_name:
642 * Get the name of this kmod module. Name is always available, independently
643 * if it was created by kmod_module_new_from_name() or another function and
644 * it's always normalized (dashes are replaced with underscores).
646 * Returns: the name of this kmod module.
648 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
654 * kmod_module_get_path:
657 * Get the path of this kmod module. If this kmod module was not created by
658 * path, it can search the modules.dep index in order to find out the module
659 * under context's dirname (see kmod_get_dirname()).
661 * Returns: the path of this kmod module or NULL if such information is not
664 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
668 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
670 if (mod
->path
!= NULL
)
676 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
680 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
687 extern long delete_module(const char *name
, unsigned int flags
);
690 * kmod_module_remove_module:
692 * @flags: flags to pass to Linux kernel when removing the module
694 * Remove a module from Linux kernel.
696 * Returns: 0 on success or < 0 on failure.
698 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
706 /* Filter out other flags */
707 flags
&= (KMOD_REMOVE_FORCE
| KMOD_REMOVE_NOWAIT
);
709 err
= delete_module(mod
->name
, flags
);
711 ERR(mod
->ctx
, "Removing '%s': %s\n", mod
->name
,
719 extern long init_module(void *mem
, unsigned long len
, const char *args
);
722 * kmod_module_insert_module:
724 * @flags: flags are not passed to Linux Kernel, but instead it dictates the
725 * behavior of this function. They are not implemented yet.
726 * @options: module's options to pass to Linux Kernel.
728 * Insert a module in Linux kernel. It opens the file pointed by @mod,
729 * mmap'ing it and passing to kernel.
731 * Returns: 0 on success or < 0 on failure.
733 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
741 const char *args
= options
? options
: "";
746 if (mod
->path
== NULL
) {
747 ERR(mod
->ctx
, "Not supported to load a module by name yet\n");
752 INFO(mod
->ctx
, "Flags are not implemented yet\n");
754 if ((fd
= open(mod
->path
, O_RDONLY
)) < 0) {
761 if ((mmaped_file
= mmap(0, st
.st_size
, PROT_READ
,
762 MAP_PRIVATE
, fd
, 0)) == MAP_FAILED
) {
767 err
= init_module(mmaped_file
, st
.st_size
, args
);
769 ERR(mod
->ctx
, "Failed to insert module '%s'\n", mod
->path
);
771 munmap(mmaped_file
, st
.st_size
);
778 * kmod_module_get_options:
781 * Get options of this kmod module. Options come from the configuration file
782 * and are cached in @mod. The first call to this function will search for
783 * this module in configuration and subsequent calls return the cached string.
785 * Returns: a string with all the options separated by spaces. This string is
786 * owned by @mod, do not free it.
788 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
793 if (!mod
->init
.options
) {
795 struct kmod_module
*m
= (struct kmod_module
*)mod
;
796 const struct kmod_list
*l
, *ctx_options
;
800 ctx_options
= kmod_get_options(mod
->ctx
);
802 kmod_list_foreach(l
, ctx_options
) {
803 const char *modname
= kmod_option_get_modname(l
);
808 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
809 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
810 streq(modname
, mod
->alias
))))
813 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
814 str
= kmod_option_get_options(l
);
819 tmp
= realloc(opts
, optslen
+ len
+ 2);
832 memcpy(opts
+ optslen
, str
, len
);
834 opts
[optslen
] = '\0';
837 m
->init
.options
= true;
844 ERR(mod
->ctx
, "out of memory\n");
849 * kmod_module_get_install_commands:
852 * Get install commands for this kmod module. Install commands come from the
853 * configuration file and are cached in @mod. The first call to this function
854 * will search for this module in configuration and subsequent calls return
855 * the cached string. The install commands are returned as they were in the
856 * configuration, concatenated by ';'. No other processing is made in this
859 * Returns: a string with all install commands separated by semicolons. This
860 * string is owned by @mod, do not free it.
862 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
867 if (!mod
->init
.install_commands
) {
869 struct kmod_module
*m
= (struct kmod_module
*)mod
;
870 const struct kmod_list
*l
, *ctx_install_commands
;
874 ctx_install_commands
= kmod_get_install_commands(mod
->ctx
);
876 kmod_list_foreach(l
, ctx_install_commands
) {
877 const char *modname
= kmod_command_get_modname(l
);
882 if (strcmp(modname
, mod
->name
) != 0)
885 str
= kmod_command_get_command(l
);
890 tmp
= realloc(cmds
, cmdslen
+ len
+ 2);
903 memcpy(cmds
+ cmdslen
, str
, len
);
905 cmds
[cmdslen
] = '\0';
908 m
->init
.install_commands
= true;
909 m
->install_commands
= cmds
;
912 return mod
->install_commands
;
915 ERR(mod
->ctx
, "out of memory\n");
920 * kmod_module_get_remove_commands:
923 * Get remove commands for this kmod module. Remove commands come from the
924 * configuration file and are cached in @mod. The first call to this function
925 * will search for this module in configuration and subsequent calls return
926 * the cached string. The remove commands are returned as they were in the
927 * configuration, concatenated by ';'. No other processing is made in this
930 * Returns: a string with all remove commands separated by semicolons. This
931 * string is owned by @mod, do not free it.
933 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
938 if (!mod
->init
.remove_commands
) {
940 struct kmod_module
*m
= (struct kmod_module
*)mod
;
941 const struct kmod_list
*l
, *ctx_remove_commands
;
945 ctx_remove_commands
= kmod_get_remove_commands(mod
->ctx
);
947 kmod_list_foreach(l
, ctx_remove_commands
) {
948 const char *modname
= kmod_command_get_modname(l
);
953 if (strcmp(modname
, mod
->name
) != 0)
956 str
= kmod_command_get_command(l
);
961 tmp
= realloc(cmds
, cmdslen
+ len
+ 2);
974 memcpy(cmds
+ cmdslen
, str
, len
);
976 cmds
[cmdslen
] = '\0';
979 m
->init
.remove_commands
= true;
980 m
->remove_commands
= cmds
;
983 return mod
->remove_commands
;
986 ERR(mod
->ctx
, "out of memory\n");
991 * SECTION:libkmod-loaded
992 * @short_description: currently loaded modules
994 * Information about currently loaded modules, as reported by Linux kernel.
995 * These information are not cached by libkmod and are always read from /sys
1000 * kmod_module_new_from_loaded:
1001 * @ctx: kmod library context
1002 * @list: where to save the list of loaded modules
1004 * Create a new list of kmod modules with all modules currently loaded in
1005 * kernel. It uses /proc/modules to get the names of loaded modules and to
1006 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1007 * They are put are put in @list in no particular order.
1009 * The initial refcount is 1, and needs to be decremented to release the
1010 * resources of the kmod_module. The returned @list must be released by
1011 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1012 * kmod_modules created, they are all released upon @ctx destruction too. Do
1013 * not unref @ctx before all the desired operations with the returned list are
1016 * Returns: 0 on success or < 0 on error.
1018 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1019 struct kmod_list
**list
)
1021 struct kmod_list
*l
= NULL
;
1025 if (ctx
== NULL
|| list
== NULL
)
1028 fp
= fopen("/proc/modules", "r");
1031 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1035 while (fgets(line
, sizeof(line
), fp
)) {
1036 struct kmod_module
*m
;
1037 struct kmod_list
*node
;
1039 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1041 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1043 ERR(ctx
, "could not get module from name '%s': %s\n",
1044 name
, strerror(-err
));
1048 node
= kmod_list_append(l
, m
);
1052 ERR(ctx
, "out of memory\n");
1053 kmod_module_unref(m
);
1064 * kmod_module_initstate_str:
1065 * @state: the state as returned by kmod_module_get_initstate()
1067 * Translate a initstate to a string.
1069 * Returns: the string associated to the @state. This string is statically
1070 * allocated, do not free it.
1072 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1075 case KMOD_MODULE_BUILTIN
:
1077 case KMOD_MODULE_LIVE
:
1079 case KMOD_MODULE_COMING
:
1081 case KMOD_MODULE_GOING
:
1089 * kmod_module_get_initstate:
1092 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1095 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1097 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1099 char path
[PATH_MAX
], buf
[32];
1100 int fd
, err
, pathlen
;
1102 pathlen
= snprintf(path
, sizeof(path
),
1103 "/sys/module/%s/initstate", mod
->name
);
1104 fd
= open(path
, O_RDONLY
);
1108 if (pathlen
> (int)sizeof("/initstate") - 1) {
1110 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1111 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1112 return KMOD_MODULE_BUILTIN
;
1115 DBG(mod
->ctx
, "could not open '%s': %s\n",
1116 path
, strerror(-err
));
1120 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1123 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1124 path
, strerror(-err
));
1128 if (streq(buf
, "live\n"))
1129 return KMOD_MODULE_LIVE
;
1130 else if (streq(buf
, "coming\n"))
1131 return KMOD_MODULE_COMING
;
1132 else if (streq(buf
, "going\n"))
1133 return KMOD_MODULE_GOING
;
1135 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1140 * kmod_module_get_refcnt:
1143 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1146 * Returns: 0 on success or < 0 on failure.
1148 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1150 char path
[PATH_MAX
];
1154 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1155 fd
= open(path
, O_RDONLY
);
1158 ERR(mod
->ctx
, "could not open '%s': %s\n",
1159 path
, strerror(errno
));
1163 err
= read_str_long(fd
, &refcnt
, 10);
1166 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1167 path
, strerror(-err
));
1175 * kmod_module_get_holders:
1178 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1179 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1181 * Returns: a new list of kmod modules on success or NULL on failure.
1183 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1185 char dname
[PATH_MAX
];
1186 struct kmod_list
*list
= NULL
;
1191 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1195 ERR(mod
->ctx
, "could not open '%s': %s\n",
1196 dname
, strerror(errno
));
1201 struct dirent de
, *entp
;
1202 struct kmod_module
*holder
;
1203 struct kmod_list
*l
;
1206 err
= readdir_r(d
, &de
, &entp
);
1208 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1209 mod
->name
, strerror(-err
));
1216 if (de
.d_name
[0] == '.') {
1217 if (de
.d_name
[1] == '\0' ||
1218 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1222 err
= kmod_module_new_from_name(mod
->ctx
, de
.d_name
, &holder
);
1224 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1225 de
.d_name
, strerror(-err
));
1229 l
= kmod_list_append(list
, holder
);
1233 ERR(mod
->ctx
, "out of memory\n");
1234 kmod_module_unref(holder
);
1244 kmod_module_unref_list(list
);
1248 struct kmod_module_section
{
1249 unsigned long address
;
1253 static void kmod_module_section_free(struct kmod_module_section
*section
)
1259 * kmod_module_get_sections:
1262 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1263 * structure contained in this list is internal to libkmod and their fields
1264 * can be obtained by calling kmod_module_section_get_name() and
1265 * kmod_module_section_get_address().
1267 * After use, free the @list by calling kmod_module_section_free_list().
1269 * Returns: a new list of kmod module sections on success or NULL on failure.
1271 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1273 char dname
[PATH_MAX
];
1274 struct kmod_list
*list
= NULL
;
1281 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1285 ERR(mod
->ctx
, "could not open '%s': %s\n",
1286 dname
, strerror(errno
));
1293 struct dirent de
, *entp
;
1294 struct kmod_module_section
*section
;
1295 struct kmod_list
*l
;
1296 unsigned long address
;
1300 err
= readdir_r(d
, &de
, &entp
);
1302 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1303 mod
->name
, strerror(-err
));
1307 if (de
.d_name
[0] == '.') {
1308 if (de
.d_name
[1] == '\0' ||
1309 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1313 fd
= openat(dfd
, de
.d_name
, O_RDONLY
);
1315 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
1320 err
= read_str_ulong(fd
, &address
, 16);
1323 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
1328 namesz
= strlen(de
.d_name
) + 1;
1329 section
= malloc(sizeof(*section
) + namesz
);
1331 if (section
== NULL
) {
1332 ERR(mod
->ctx
, "out of memory\n");
1336 section
->address
= address
;
1337 memcpy(section
->name
, de
.d_name
, namesz
);
1339 l
= kmod_list_append(list
, section
);
1343 ERR(mod
->ctx
, "out of memory\n");
1354 kmod_module_unref_list(list
);
1359 * kmod_module_section_get_module_name:
1360 * @entry: a list entry representing a kmod module section
1362 * Get the name of a kmod module section.
1364 * After use, free the @list by calling kmod_module_section_free_list().
1366 * Returns: the name of this kmod module section on success or NULL on
1367 * failure. The string is owned by the section, do not free it.
1369 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
1371 struct kmod_module_section
*section
;
1376 section
= entry
->data
;
1377 return section
->name
;
1381 * kmod_module_section_get_address:
1382 * @entry: a list entry representing a kmod module section
1384 * Get the address of a kmod module section.
1386 * After use, free the @list by calling kmod_module_section_free_list().
1388 * Returns: the address of this kmod module section on success or ULONG_MAX
1391 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
1393 struct kmod_module_section
*section
;
1396 return (unsigned long)-1;
1398 section
= entry
->data
;
1399 return section
->address
;
1403 * kmod_module_section_free_list:
1404 * @list: kmod module section list
1406 * Release the resources taken by @list
1408 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
1411 kmod_module_section_free(list
->data
);
1412 list
= kmod_list_remove(list
);