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.
51 struct kmod_list
*dep
;
53 char *install_commands
;
54 char *remove_commands
;
55 char *alias
; /* only set if this module was created from an alias */
61 bool install_commands
: 1;
62 bool remove_commands
: 1;
66 inline char *modname_normalize(const char *modname
, char buf
[NAME_MAX
],
71 for (s
= 0; s
< NAME_MAX
- 1; s
++) {
72 const char c
= modname
[s
];
75 else if (c
== '\0' || c
== '.')
89 static char *path_to_modname(const char *path
, char buf
[NAME_MAX
], size_t *len
)
93 modname
= basename(path
);
94 if (modname
== NULL
|| modname
[0] == '\0')
97 return modname_normalize(modname
, buf
, len
);
100 static inline const char *path_join(const char *path
, size_t prefixlen
,
108 pathlen
= strlen(path
);
109 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
112 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
116 int kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
118 struct kmod_ctx
*ctx
= mod
->ctx
;
119 struct kmod_list
*list
= NULL
;
128 assert(mod
->dep
== NULL
);
129 mod
->init
.dep
= true;
131 p
= strchr(line
, ':');
136 dirname
= kmod_get_dirname(mod
->ctx
);
137 dirnamelen
= strlen(dirname
);
138 if (dirnamelen
+ 2 >= PATH_MAX
)
141 memcpy(buf
, dirname
, dirnamelen
);
142 buf
[dirnamelen
] = '/';
144 buf
[dirnamelen
] = '\0';
146 if (mod
->path
== NULL
) {
147 const char *str
= path_join(line
, dirnamelen
, buf
);
150 mod
->path
= strdup(str
);
151 if (mod
->path
== NULL
)
156 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
157 p
= strtok_r(NULL
, " \t", &saveptr
)) {
158 struct kmod_module
*depmod
;
161 path
= path_join(p
, dirnamelen
, buf
);
163 ERR(ctx
, "could not join path '%s' and '%s'.\n",
168 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
170 ERR(ctx
, "ctx=%p path=%s error=%s\n",
171 ctx
, path
, strerror(-err
));
175 DBG(ctx
, "add dep: %s\n", path
);
177 list
= kmod_list_append(list
, depmod
);
181 DBG(ctx
, "%d dependencies for %s\n", n
, mod
->name
);
188 kmod_module_unref_list(list
);
189 mod
->init
.dep
= false;
194 * kmod_module_new_from_name:
195 * @ctx: kmod library context
196 * @name: name of the module
197 * @mod: where to save the created struct kmod_module
199 * Create a new struct kmod_module using the module name. @name can not be an
200 * alias, file name or anything else; it must be a module name. There's no
201 * check if the module does exists in the system.
203 * This function is also used internally by many others that return a new
204 * struct kmod_module or a new list of modules.
206 * The initial refcount is 1, and needs to be decremented to release the
207 * resources of the kmod_module. Since libkmod keeps track of all
208 * kmod_modules created, they are all released upon @ctx destruction too. Do
209 * not unref @ctx before all the desired operations with the returned
210 * kmod_module are done.
212 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
213 * module name or if memory allocation failed.
215 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
217 struct kmod_module
**mod
)
219 struct kmod_module
*m
;
221 char name_norm
[NAME_MAX
];
224 if (ctx
== NULL
|| name
== NULL
)
227 alias_normalize(name
, name_norm
, &namelen
);
229 m
= kmod_pool_get_module(ctx
, name_norm
);
231 *mod
= kmod_module_ref(m
);
235 namesep
= strchr(name_norm
, '/');
236 m
= malloc(sizeof(*m
) + (namesep
== NULL
? 1 : 2) * namelen
+ 2);
242 memset(m
, 0, sizeof(*m
));
244 m
->ctx
= kmod_ref(ctx
);
245 m
->name
= (char *)m
+ sizeof(*m
);
246 memcpy(m
->name
, name_norm
, namelen
+ 1);
249 size_t len
= namesep
- name_norm
;
252 m
->alias
= m
->name
+ len
+ 1;
253 m
->hashkey
= m
->name
+ namelen
+ 1;
254 memcpy(m
->hashkey
, name_norm
, namelen
+ 1);
256 m
->hashkey
= m
->name
;
260 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
266 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
267 const char *name
, struct kmod_module
**mod
)
271 size_t namelen
= strlen(name
);
272 size_t aliaslen
= strlen(alias
);
274 if (namelen
+ aliaslen
+ 2 > NAME_MAX
)
275 return -ENAMETOOLONG
;
277 memcpy(key
, name
, namelen
);
278 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
281 err
= kmod_module_new_from_name(ctx
, key
, mod
);
289 * kmod_module_new_from_path:
290 * @ctx: kmod library context
291 * @path: path where to find the given module
292 * @mod: where to save the created struct kmod_module
294 * Create a new struct kmod_module using the module path. @path must be an
295 * existent file with in the filesystem and must be accessible to libkmod.
297 * The initial refcount is 1, and needs to be decremented to release the
298 * resources of the kmod_module. Since libkmod keeps track of all
299 * kmod_modules created, they are all released upon @ctx destruction too. Do
300 * not unref @ctx before all the desired operations with the returned
301 * kmod_module are done.
303 * If @path is relative, it's treated as relative to the current working
304 * directory. Otherwise, give an absolute path.
306 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
307 * it's not a valid file for a kmod_module or if memory allocation failed.
309 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
311 struct kmod_module
**mod
)
313 struct kmod_module
*m
;
320 if (ctx
== NULL
|| path
== NULL
)
323 abspath
= path_make_absolute_cwd(path
);
327 err
= stat(abspath
, &st
);
333 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
338 m
= kmod_pool_get_module(ctx
, name
);
342 else if (streq(m
->path
, abspath
))
345 ERR(ctx
, "kmod_module '%s' already exists with different path\n",
351 *mod
= kmod_module_ref(m
);
355 m
= malloc(sizeof(*m
) + namelen
+ 1);
359 memset(m
, 0, sizeof(*m
));
361 m
->ctx
= kmod_ref(ctx
);
362 m
->name
= (char *)m
+ sizeof(*m
);
363 memcpy(m
->name
, name
, namelen
);
365 m
->hashkey
= m
->name
;
368 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
379 * Drop a reference of the kmod module. If the refcount reaches zero, its
380 * resources are released.
382 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
383 * returns the passed @mod with its refcount decremented.
385 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
390 if (--mod
->refcount
> 0)
393 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
395 kmod_module_unref_list(mod
->dep
);
396 kmod_unref(mod
->ctx
);
398 free(mod
->install_commands
);
399 free(mod
->remove_commands
);
409 * Take a reference of the kmod module, incrementing its refcount.
411 * Returns: the passed @module with its refcount incremented.
413 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
423 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
427 if (*(_list) != NULL) \
432 * kmod_module_new_from_lookup:
433 * @ctx: kmod library context
434 * @given_alias: alias to look for
435 * @list: an empty list where to save the list of modules matching
438 * Create a new list of kmod modules using an alias or module name and lookup
439 * libkmod's configuration files and indexes in order to find the module.
440 * Once it's found in one of the places, it stops searching and create the
441 * list of modules that is saved in @list.
443 * The search order is: 1. aliases in configuration file; 2. module names in
444 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
445 * in modules.alias index.
447 * The initial refcount is 1, and needs to be decremented to release the
448 * resources of the kmod_module. The returned @list must be released by
449 * calling kmod_module_unref_list(). Since libkmod keeps track of all
450 * kmod_modules created, they are all released upon @ctx destruction too. Do
451 * not unref @ctx before all the desired operations with the returned list are
454 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
455 * methods failed, which is basically due to memory allocation fail. If module
456 * is not found, it still returns 0, but @list is an empty list.
458 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
459 const char *given_alias
,
460 struct kmod_list
**list
)
463 char alias
[NAME_MAX
];
465 if (ctx
== NULL
|| given_alias
== NULL
)
468 if (list
== NULL
|| *list
!= NULL
) {
469 ERR(ctx
, "An empty list is needed to create lookup\n");
473 if (alias_normalize(given_alias
, alias
, NULL
) < 0)
476 /* Aliases from config file override all the others */
477 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
478 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
480 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
481 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
483 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
484 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
486 // TODO: add lookup for install commands here.
488 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
489 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
495 kmod_module_unref_list(*list
);
499 #undef CHECK_ERR_AND_FINISH
502 * kmod_module_unref_list:
503 * @list: list of kmod modules
505 * Drop a reference of each kmod module in @list and releases the resources
506 * taken by the list itself.
508 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
509 * returns the passed @mod with its refcount decremented.
511 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
513 for (; list
!= NULL
; list
= kmod_list_remove(list
))
514 kmod_module_unref(list
->data
);
520 * kmod_module_get_dependencies:
523 * Search the modules.dep index to find the dependencies of the given @mod.
524 * The result is cached in @mod, so subsequent calls to this function will
525 * return the already searched list of modules.
527 * Returns: NULL on failure or if there are any dependencies. Otherwise it
528 * returns a list of kmod modules that can be released by calling
529 * kmod_module_unref_list().
531 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
533 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
538 if (!mod
->init
.dep
) {
540 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
545 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
552 kmod_list_foreach(l
, mod
->dep
) {
553 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
555 kmod_module_unref(l
->data
);
565 ERR(mod
->ctx
, "out of memory\n");
566 kmod_module_unref_list(list_new
);
571 * kmod_module_get_module:
572 * @entry: an entry in a list of kmod modules.
574 * Get the kmod module of this @entry in the list, increasing its refcount.
575 * After it's used, unref it. Since the refcount is incremented upon return,
576 * you still have to call kmod_module_unref_list() to release the list of kmod
579 * Returns: NULL on failure or the kmod_module contained in this list entry
580 * with its refcount incremented.
582 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
587 return kmod_module_ref(entry
->data
);
591 * kmod_module_get_name:
594 * Get the name of this kmod module. Name is always available, independently
595 * if it was created by kmod_module_new_from_name() or another function and
596 * it's always normalized (dashes are replaced with underscores).
598 * Returns: the name of this kmod module.
600 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
606 * kmod_module_get_path:
609 * Get the path of this kmod module. If this kmod module was not created by
610 * path, it can search the modules.dep index in order to find out the module
611 * under context's dirname (see kmod_get_dirname()).
613 * Returns: the path of this kmod module or NULL if such information is not
616 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
620 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
622 if (mod
->path
!= NULL
)
628 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
632 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
639 extern long delete_module(const char *name
, unsigned int flags
);
642 * kmod_module_remove_module:
644 * @flags: flags to pass to Linux kernel when removing the module
646 * Remove a module from Linux kernel.
648 * Returns: 0 on success or < 0 on failure.
650 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
658 /* Filter out other flags */
659 flags
&= (KMOD_REMOVE_FORCE
| KMOD_REMOVE_NOWAIT
);
661 err
= delete_module(mod
->name
, flags
);
663 ERR(mod
->ctx
, "Could not remove '%s': %s\n", mod
->name
,
671 extern long init_module(void *mem
, unsigned long len
, const char *args
);
674 * kmod_module_insert_module:
676 * @flags: flags are not passed to Linux Kernel, but instead it dictates the
677 * behavior of this function. They are not implemented yet.
678 * @options: module's options to pass to Linux Kernel.
680 * Insert a module in Linux kernel. It opens the file pointed by @mod,
681 * mmap'ing it and passing to kernel.
683 * Returns: 0 on success or < 0 on failure.
685 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
693 const char *args
= options
? options
: "";
698 if (mod
->path
== NULL
) {
699 ERR(mod
->ctx
, "Not supported to load a module by name yet\n");
704 INFO(mod
->ctx
, "Flags are not implemented yet\n");
706 if ((fd
= open(mod
->path
, O_RDONLY
)) < 0) {
713 if ((mmaped_file
= mmap(0, st
.st_size
, PROT_READ
,
714 MAP_PRIVATE
, fd
, 0)) == MAP_FAILED
) {
719 err
= init_module(mmaped_file
, st
.st_size
, args
);
721 ERR(mod
->ctx
, "Failed to insert module '%s'\n", mod
->path
);
723 munmap(mmaped_file
, st
.st_size
);
730 * kmod_module_get_options:
733 * Get options of this kmod module. Options come from the configuration file
734 * and are cached in @mod. The first call to this function will search for
735 * this module in configuration and subsequent calls return the cached string.
737 * Returns: a string with all the options separated by spaces. This string is
738 * owned by @mod, do not free it.
740 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
745 if (!mod
->init
.options
) {
747 struct kmod_module
*m
= (struct kmod_module
*)mod
;
748 const struct kmod_list
*l
, *ctx_options
;
752 ctx_options
= kmod_get_options(mod
->ctx
);
754 kmod_list_foreach(l
, ctx_options
) {
755 const char *modname
= kmod_option_get_modname(l
);
760 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
761 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
762 streq(modname
, mod
->alias
))))
765 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
766 str
= kmod_option_get_options(l
);
771 tmp
= realloc(opts
, optslen
+ len
+ 2);
784 memcpy(opts
+ optslen
, str
, len
);
786 opts
[optslen
] = '\0';
789 m
->init
.options
= true;
796 ERR(mod
->ctx
, "out of memory\n");
801 * kmod_module_get_install_commands:
804 * Get install commands for this kmod module. Install commands come from the
805 * configuration file and are cached in @mod. The first call to this function
806 * will search for this module in configuration and subsequent calls return
807 * the cached string. The install commands are returned as they were in the
808 * configuration, concatenated by ';'. No other processing is made in this
811 * Returns: a string with all install commands separated by semicolons. This
812 * string is owned by @mod, do not free it.
814 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
819 if (!mod
->init
.install_commands
) {
821 struct kmod_module
*m
= (struct kmod_module
*)mod
;
822 const struct kmod_list
*l
, *ctx_install_commands
;
826 ctx_install_commands
= kmod_get_install_commands(mod
->ctx
);
828 kmod_list_foreach(l
, ctx_install_commands
) {
829 const char *modname
= kmod_command_get_modname(l
);
834 if (strcmp(modname
, mod
->name
) != 0)
837 str
= kmod_command_get_command(l
);
842 tmp
= realloc(cmds
, cmdslen
+ len
+ 2);
855 memcpy(cmds
+ cmdslen
, str
, len
);
857 cmds
[cmdslen
] = '\0';
860 m
->init
.install_commands
= true;
861 m
->install_commands
= cmds
;
864 return mod
->install_commands
;
867 ERR(mod
->ctx
, "out of memory\n");
872 * kmod_module_get_remove_commands:
875 * Get remove commands for this kmod module. Remove commands come from the
876 * configuration file and are cached in @mod. The first call to this function
877 * will search for this module in configuration and subsequent calls return
878 * the cached string. The remove commands are returned as they were in the
879 * configuration, concatenated by ';'. No other processing is made in this
882 * Returns: a string with all remove commands separated by semicolons. This
883 * string is owned by @mod, do not free it.
885 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
890 if (!mod
->init
.remove_commands
) {
892 struct kmod_module
*m
= (struct kmod_module
*)mod
;
893 const struct kmod_list
*l
, *ctx_remove_commands
;
897 ctx_remove_commands
= kmod_get_remove_commands(mod
->ctx
);
899 kmod_list_foreach(l
, ctx_remove_commands
) {
900 const char *modname
= kmod_command_get_modname(l
);
905 if (strcmp(modname
, mod
->name
) != 0)
908 str
= kmod_command_get_command(l
);
913 tmp
= realloc(cmds
, cmdslen
+ len
+ 2);
926 memcpy(cmds
+ cmdslen
, str
, len
);
928 cmds
[cmdslen
] = '\0';
931 m
->init
.remove_commands
= true;
932 m
->remove_commands
= cmds
;
935 return mod
->remove_commands
;
938 ERR(mod
->ctx
, "out of memory\n");
943 * SECTION:libkmod-loaded
944 * @short_description: currently loaded modules
946 * Information about currently loaded modules, as reported by Linux kernel.
947 * These information are not cached by libkmod and are always read from /sys
952 * kmod_module_new_from_loaded:
953 * @ctx: kmod library context
954 * @list: where to save the list of loaded modules
956 * Create a new list of kmod modules with all modules currently loaded in
957 * kernel. It uses /proc/modules to get the names of loaded modules and to
958 * create kmod modules by calling kmod_module_new_from_name() in each of them.
959 * They are put are put in @list in no particular order.
961 * The initial refcount is 1, and needs to be decremented to release the
962 * resources of the kmod_module. The returned @list must be released by
963 * calling kmod_module_unref_list(). Since libkmod keeps track of all
964 * kmod_modules created, they are all released upon @ctx destruction too. Do
965 * not unref @ctx before all the desired operations with the returned list are
968 * Returns: 0 on success or < 0 on error.
970 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
971 struct kmod_list
**list
)
973 struct kmod_list
*l
= NULL
;
977 if (ctx
== NULL
|| list
== NULL
)
980 fp
= fopen("/proc/modules", "r");
983 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
987 while (fgets(line
, sizeof(line
), fp
)) {
988 struct kmod_module
*m
;
989 struct kmod_list
*node
;
991 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
993 err
= kmod_module_new_from_name(ctx
, name
, &m
);
995 ERR(ctx
, "could not get module from name '%s': %s\n",
996 name
, strerror(-err
));
1000 node
= kmod_list_append(l
, m
);
1004 ERR(ctx
, "out of memory\n");
1005 kmod_module_unref(m
);
1016 * kmod_module_initstate_str:
1017 * @state: the state as returned by kmod_module_get_initstate()
1019 * Translate a initstate to a string.
1021 * Returns: the string associated to the @state. This string is statically
1022 * allocated, do not free it.
1024 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1027 case KMOD_MODULE_BUILTIN
:
1029 case KMOD_MODULE_LIVE
:
1031 case KMOD_MODULE_COMING
:
1033 case KMOD_MODULE_GOING
:
1041 * kmod_module_get_initstate:
1044 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1047 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1049 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1051 char path
[PATH_MAX
], buf
[32];
1052 int fd
, err
, pathlen
;
1054 pathlen
= snprintf(path
, sizeof(path
),
1055 "/sys/module/%s/initstate", mod
->name
);
1056 fd
= open(path
, O_RDONLY
);
1060 if (pathlen
> (int)sizeof("/initstate") - 1) {
1062 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1063 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1064 return KMOD_MODULE_BUILTIN
;
1067 DBG(mod
->ctx
, "could not open '%s': %s\n",
1068 path
, strerror(-err
));
1072 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1075 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1076 path
, strerror(-err
));
1080 if (streq(buf
, "live\n"))
1081 return KMOD_MODULE_LIVE
;
1082 else if (streq(buf
, "coming\n"))
1083 return KMOD_MODULE_COMING
;
1084 else if (streq(buf
, "going\n"))
1085 return KMOD_MODULE_GOING
;
1087 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1092 * kmod_module_get_size:
1095 * Get the size of this kmod module as returned by Linux kernel. It reads the
1096 * file /proc/modules to search for this module and get its size.
1098 * Returns: the size of this kmod module.
1100 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1102 // FIXME TODO: this should be available from /sys/module/foo
1106 long size
= -ENOENT
;
1111 fp
= fopen("/proc/modules", "r");
1115 "could not open /proc/modules: %s\n", strerror(errno
));
1119 while (fgets(line
, sizeof(line
), fp
)) {
1120 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1124 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1127 tok
= strtok_r(NULL
, " \t", &saveptr
);
1130 "invalid line format at /proc/modules:%d\n", lineno
);
1134 value
= strtol(tok
, &endptr
, 10);
1135 if (endptr
== tok
|| *endptr
!= '\0') {
1137 "invalid line format at /proc/modules:%d\n", lineno
);
1149 * kmod_module_get_refcnt:
1152 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1155 * Returns: 0 on success or < 0 on failure.
1157 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1159 char path
[PATH_MAX
];
1163 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1164 fd
= open(path
, O_RDONLY
);
1167 ERR(mod
->ctx
, "could not open '%s': %s\n",
1168 path
, strerror(errno
));
1172 err
= read_str_long(fd
, &refcnt
, 10);
1175 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1176 path
, strerror(-err
));
1184 * kmod_module_get_holders:
1187 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1188 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1190 * Returns: a new list of kmod modules on success or NULL on failure.
1192 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1194 char dname
[PATH_MAX
];
1195 struct kmod_list
*list
= NULL
;
1200 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1204 ERR(mod
->ctx
, "could not open '%s': %s\n",
1205 dname
, strerror(errno
));
1210 struct dirent de
, *entp
;
1211 struct kmod_module
*holder
;
1212 struct kmod_list
*l
;
1215 err
= readdir_r(d
, &de
, &entp
);
1217 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1218 mod
->name
, strerror(-err
));
1225 if (de
.d_name
[0] == '.') {
1226 if (de
.d_name
[1] == '\0' ||
1227 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1231 err
= kmod_module_new_from_name(mod
->ctx
, de
.d_name
, &holder
);
1233 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1234 de
.d_name
, strerror(-err
));
1238 l
= kmod_list_append(list
, holder
);
1242 ERR(mod
->ctx
, "out of memory\n");
1243 kmod_module_unref(holder
);
1253 kmod_module_unref_list(list
);
1257 struct kmod_module_section
{
1258 unsigned long address
;
1262 static void kmod_module_section_free(struct kmod_module_section
*section
)
1268 * kmod_module_get_sections:
1271 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1272 * structure contained in this list is internal to libkmod and their fields
1273 * can be obtained by calling kmod_module_section_get_name() and
1274 * kmod_module_section_get_address().
1276 * After use, free the @list by calling kmod_module_section_free_list().
1278 * Returns: a new list of kmod module sections on success or NULL on failure.
1280 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1282 char dname
[PATH_MAX
];
1283 struct kmod_list
*list
= NULL
;
1290 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1294 ERR(mod
->ctx
, "could not open '%s': %s\n",
1295 dname
, strerror(errno
));
1302 struct dirent de
, *entp
;
1303 struct kmod_module_section
*section
;
1304 struct kmod_list
*l
;
1305 unsigned long address
;
1309 err
= readdir_r(d
, &de
, &entp
);
1311 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1312 mod
->name
, strerror(-err
));
1316 if (de
.d_name
[0] == '.') {
1317 if (de
.d_name
[1] == '\0' ||
1318 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1322 fd
= openat(dfd
, de
.d_name
, O_RDONLY
);
1324 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
1329 err
= read_str_ulong(fd
, &address
, 16);
1332 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
1337 namesz
= strlen(de
.d_name
) + 1;
1338 section
= malloc(sizeof(*section
) + namesz
);
1340 if (section
== NULL
) {
1341 ERR(mod
->ctx
, "out of memory\n");
1345 section
->address
= address
;
1346 memcpy(section
->name
, de
.d_name
, namesz
);
1348 l
= kmod_list_append(list
, section
);
1352 ERR(mod
->ctx
, "out of memory\n");
1363 kmod_module_unref_list(list
);
1368 * kmod_module_section_get_module_name:
1369 * @entry: a list entry representing a kmod module section
1371 * Get the name of a kmod module section.
1373 * After use, free the @list by calling kmod_module_section_free_list().
1375 * Returns: the name of this kmod module section on success or NULL on
1376 * failure. The string is owned by the section, do not free it.
1378 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
1380 struct kmod_module_section
*section
;
1385 section
= entry
->data
;
1386 return section
->name
;
1390 * kmod_module_section_get_address:
1391 * @entry: a list entry representing a kmod module section
1393 * Get the address of a kmod module section.
1395 * After use, free the @list by calling kmod_module_section_free_list().
1397 * Returns: the address of this kmod module section on success or ULONG_MAX
1400 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
1402 struct kmod_module_section
*section
;
1405 return (unsigned long)-1;
1407 section
= entry
->data
;
1408 return section
->address
;
1412 * kmod_module_section_free_list:
1413 * @list: kmod module section list
1415 * Release the resources taken by @list
1417 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
1420 kmod_module_section_free(list
->data
);
1421 list
= kmod_list_remove(list
);