Module was never being removed from hash table. Therefore, if we create
a module, unref it and create it again we will access freed memory.
Commit "53385cf Improve test of double references" introduced a new test
in test-mod-double-ref.c that previously to this commit was crashing and
now it's working fine.
The hash key is not copied so we can't change the string from:
modname/modalias
to:
modname'\0'modalias
in order to setup mod->name and mod->alias.
Now what we do is:
1) if name is in the form 'modname/modalias', the final struct
kmod_module will be:
struct kmod_module {
char *alias;------,
char *name;-----, |
char *hashkey;--|-|-,
} | | |
name <------------------' | |
alias <-------------------' |
hashkey <-------------------'
2) if name is in the simple form 'modname', then the final struct
kmod_module will be:
struct kmod_module {
char *alias;------> NULL
char *name;-----,
char *hashkey;--|---,
} | |
name <------------------*---'
1 alias may correspond to more than 1 module. This would cause a
conflict in the hash table when inserting a module there and bad things
could happen.
Now we use 'modname/aliasname' as key, '/aliasname' part being optional.
Internally kmod_module_new_from_alias() will setup a 'modname/aliasname'
string and pass to kmod_module_new_from_name() that will treat the case
with a '/' in the name.
User might call kmod_module_new_from_name() without any slashes, so the
key my not contain it.
We have a static vector of indexes. There's no need to check if they
have a suffix or if they are absolute, we just already know in advance
and there are no plans to change it.
Remove function kmod_resolve_alias_options since it's not needed
anymore. Test is using the following configuration file:
alias blablabla ac
options ac test=1
options blablabla test=2
Lookup test by module name:
$ ./test/test-lookup ac
libkmod version 1
Alias: 'ac'
Modules matching:
ac
options: 'test=1'
Lookup test by alias:
$ ./test/test-lookup blablabla
libkmod version 1
Alias: 'blablabla'
Modules matching:
ac
options: 'test=1 test=2'
This function will create a new kmod_module() by calling
kmod_module_new_from_name(), but instead of given the module name, it
gives the alias. This way, the modules' hash will contain the alias
instead of the name. If module was created successfully, then it swap
the alias with the actual name.
The downside is that the structure is not shared anymore if we create a
kmod_module by alias and after by name. However, in modprobe's
configuration it's possible to have different options for different
aliases. In future we might want to create a way to share the common
part of the structure (then having only one instance as we had before).
We still have name allocated just after the struct kmod_module, but
now we use a pointer instead of putting name as a vector in the end of
the structure.
The previous way has some problems, the worst is:
- it's not possible to swap the name with another value: this is
the real problem that this patch is solving. Later patches
will make name be swappable with alias, which is not possible
if name is a vector.
When normalizing alias names (or if we don't know if it's an alias or
modname), use alias_normalize() instead of modname_normalize(). The
difference is that alias names can contain dashes withing brackets, and
those should not be changed to underscores.
Most of the places using underscores() function might be converted to
alias_normalize(), but this is not done now.
It's not possible to move functions related to "live" modules to
libkmod-loaded.c because they depend on the definition of kmod_module.
Putting this structure in the private header is not a good idea, so let
all functions related to "live" information in the end of
libkmod-module.c, and move the sole function from libkmod-loaded.c to
this place. This way all functions get the right documentation
about their sections.
Lines should not go over 80 chars with a few exceptions:
- headers
- function definitions with only 1 argument
- long strings, otherwise we break grep
This should go later in a coding-style file
This will be required to implement modprobe later. The implementation
follows "man modprobe.conf" and allows options to be specified for
alias as well, thus the need for kmod_resolve_alias_options().
Example mod-a.conf:
options mod-a a=1 b=2
options mod-a c=3
alias mymod-a mod-a
options mymod-a d=4
Results in:
options mod-a a=1 b=2 c=3
options mymod-a a=1 b=2 c=3 d=4
Install commands are being concatenated with ";", but manpage is not
clean about this behavior.
For mmap mode, we can avoid allocating and copying strings from the
mmap'ed memory.
With that we have fixed length "struct index_mm_value" that can be
allocated inline with "struct index_mm_node".
uint32_t reads must be aligned, they're not then use memcpy().
read_alloc_chars_mm() and read_chars_mm() were wrong, normalize all
address calculation using single byte pointer "addr" that is
incremented by the amount read, this will avoid further errors.
Put all names in a static vector and declare a enum containing the
number of indexes. This way it's easier to create vectors inside ctx
that depend on these files.
Grow buffer based on a step, avoiding hitting the system over and over
again.
Do not allocate the 'struct buffer' as in all cases the lifetime is
known and the pattern was allocate then free in every call site.
"buf[NAME_MAX] = value" is invalid since it would access the byte
right after the array.
Also fix the const of modname, do not mess with it to avoid mistakes.
rmmod/insmod should be able to operate directly on files, not relying on
indexes and configuration.
The following test was not working and now it is:
$ # go to a dir != mod->dirname
$ cd /lib/modules/$(uname -r)/kernel
$ # try to insert module giving a relative path
$ insmod drivers/acpi/ac.ko
1) Allocate less by not sorting the result with qsort. Instead,
insert the nodes in the correct order;
2) Do not maintain the whole path in memory, but rely on openat()
kmod_module_get_dependency is renamed to kmod_module_get_dependencies
since it's returning a list. To match other APIs, now it returns a new
list that user must free with kmod_module_unref_list().
Avoid calling _next() function because it's an exported function and
linker can not optimize it.
Thanks to "Gustavo Sverzut Barbieri <barbieri@profusion.mobi>" for
suggestion.
kmod_loaded_get_list() now returns a regular list of kmod_modules, use
kmod_module_get_module(), kmod_module_unref() and
kmod_module_unref_list() to operate on it.
provide means to get:
* refcount
* initstate
* holders
* sections
this can be used to individually query properties from modules,
similar to /proc/modules (kmod_loaded / kmod_loaded_module).
make the function names reflect the structure they are operating on.
the structure is now allocated and remembers the context it was
created, then no need to give the context in every function call.
functions that do not modify their parameters get them as const pointers.
special cases:
* kmod_get_userdata/kmod_set_userdata: return as void* for user convenience.
* kmod_list_append/kmod_list_prepend: take const void* for user convenience.
We return a kmod_list when searching for an alias. Right now, it only
search for aliases in config files.
To use it, we create a list:
list = NULL;
kmod_module_new_from_lookup(..., &list);
And iterate over it to get the modules and their details:
kmod_list_foreach(l, list) {
struct kmod_mod *mod = kmod_module_get_module(l);
...
... kmod_module_get_name(mod);
... kmod_module_get_path(mod);
}
Aliases might contain globs and are match by using fnmatch().
This effectively makes the combined work be GPL. All other parts of this
library are still LGPL and if this part in future becomes
double-licensed, we can switch back to LGPL.
When mixing sscanf() and strtok() Valgrind complaints like below:
==1641== Conditional jump or move depends on uninitialised value(s)
Use stroull() instead of sscanf().