Commit Graph

85 Commits

Author SHA1 Message Date
Lucas De Marchi 0246e06340 depmod: Stop opening modules.modinfo once per module
Since the addition of modules.aliases.bin, depmod has to open that
index multiple times and parse it over and over again:

	$ sudo strace -e openat  ./tools/depmod 2>&1 | grep modules.builtin.modinfo  | wc -l
	299
	$ time sudo  ./tools/depmod
	real    0m7.814s
	user    0m7.571s
	sys     0m0.237s

Rework the logic in depmod so it does everything: open, read and parse. The
format is very straightforward and we don't need to keep it in a data structure
since we only want to add the result to a index. New output:

	$ sudo strace -e openat  ./tools/depmod 2>&1 | grep modules.builtin.modinfo  | wc -l
	1
	$ time sudo  ./tools/depmod
	real    0m7.663s
	user    0m7.516s
	sys     0m0.139s

Indexes still match:

	$ cmp /tmp/modules.builtin.alias.bin.new
	/tmp/modules.builtin.alias.bin.old; echo $?
	0

Fix: https://github.com/kmod-project/kmod/issues/11
2022-02-11 22:06:34 -08:00
Lucas De Marchi 7a0f593de3 depmod: Do not duplicate builtin index
Now that libkmod uses modules.builtin.bin again, we don't need to add
the module names in modules.builtin.alias.bin and just add the aliases.

After this change, here are the new sizes for the indexes:

Before	After	index
21k	6.4K	modules.builtin.alias.bin
11k	 11K	modules.builtin.bin
2022-02-11 22:06:34 -08:00
Lucas De Marchi f0a1360761 depmod: fix modules.builtin.alias.bin output
Due to wrong documentation on kmod_module_get_info() we ended up
checking for 0 as return. Check for > 0 to decided if we want to write
the index to the file, otherwise we would output a 0-sized index on
success.
2021-05-11 09:49:35 -07:00
Lucas De Marchi 9319b0f4cb Support /usr/local for configuration files
Add /usr/local to the search path for configuration files. These are
intended for local installs, provided /usr/local is given as prefix.
2021-01-18 18:26:36 -08:00
Lucas De Marchi 1c10f32483 depmod: fix precedence order
Configuration in /etc should have higher prio than /run.
Given how rarely configuration in /run is used with depmod, this is
likely not to cause any problems, even if it's a change in behavior.
2021-01-18 18:26:36 -08:00
Lucas De Marchi 220b4c55ec depmod: unconditionally write builtin.alias.bin
The file is always created and unless we return an error, the temporary
file is renamed to its final destination. All other places write the
index without checking if the index is empty, so just do the same.

Reported-by: Joe Buehler <aspam@cox.net>
2020-12-27 16:40:08 -08:00
Yauheni Kaliuta bd96d05256 depmod: output_builtin_alias_bin: free idx on error path
idx is allocated in the beginning but it's not freed if there is
a failure after the allocation.

Change the error path: return immediately if idx allocation fails
and then free it in both success and error path at the end.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2020-12-01 00:44:17 -08:00
Lucas De Marchi 53b30aeba2 depmod: do not output .bin to stdout
index_write() relies on fseek/ftell to manage the position to which we
are write and thus needs the file stream to support it.

Right now when trying to write the index to stdout we fail with:

	depmod: tools/depmod.c:416: index_write: Assertion `initial_offset >= 0' failed.
	Aborted (core dumped)

We have no interest in outputting our index to stdout, so just skip it
like is done with other indexes.

While at it, add/remove some newlines to improve readability.

Reported-by: Yanko Kaneti <yaneti@declera.com>
Fix: b866b2165a ("Lookup aliases in the modules.builtin.modinfo")
2020-03-13 09:23:58 -07:00
Alexey Gladkov b866b2165a Lookup aliases in the modules.builtin.modinfo
New modules.builtin.modinfo duplicates modules.builtin in the built-in
module name search. If it exists, then we can use this file, but if not,
then we need to fallback to the old file.

Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
2019-12-18 16:56:58 -08:00
Michal Suchanek 4a894aeaeb depmod: shut up gcc insufficinet buffer warning
In a couple of places depmod concatenates the module directory and filename
with snprintf. This can technically overflow creating an unterminated string if
module directory name is long. Use openat instead as is done elsewhere in
depmod. This avoids the snprintf, the extra buffer on stack, and the gcc
warning. It may even fix a corner case when the module direcotry name is just
under PATH_MAX.

[ Lucas: fix up coding style and closing fd on error path ]

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
2018-12-17 15:31:55 -08:00
Michal Suchanek a06bacf500 depmod: prevent module dependency files corruption due to parallel invocation.
Depmod does not use unique filename for temporary files. There is no
guarantee the user does not attempt to run mutiple depmod processes in
parallel. If that happens a temporary file might be created by
depmod(1st), truncated by depmod(2nd), and renamed to final name by
depmod(1st) resulting in corrupted file seen by user.

Due to missing mkstempat() this is more complex than it should be.
Adding PID and timestamp to the filename should be reasonably reliable.
Adding O_EXCL as mkstemp does fails creating the file rather than
corrupting existing file.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
2018-12-17 15:10:05 -08:00
Michal Suchanek c2996b5fa8 depmod: prevent module dependency files missing during depmod invocation
depmod deletes the module dependency files before moving the temporary
files in their place. This results in user seeing no dependency files
while they are updated. Remove the unlink call. The rename call should
suffice to move the new file in place and unlink the old one. It should
also do both atomically so there is no window when no dependency file
exists.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
2018-12-17 10:19:50 -08:00
Yauheni Kaliuta bb83f6ac68 depmod: module_is_higher_priority: fix modname length calculation
depmod_module_is_higher_priority checks module's path if it is under
module root directory and if so uses relative to the root path to
lookup the module in override and search lists.

Originally only relative path was used in the function, so the
variables with full path and and path length were changed:

       newpath += cfg->dirnamelen + 1;
       newlen -= cfg->dirnamelen + 1;
       oldpath += cfg->dirnamelen + 1;
       oldlen -= cfg->dirnamelen + 1;

Commit 7da6884e73 (depmod: implement
external directories support) changed the logic since it need the
full path to the module for comparations as well.

Unfortunately, it introduce a mistake in calculation of the relative
paths replacing '-=' with assignment to a new variable -- the
'cfg->dirnamelen + 1' value must be substracted all together. It
breaks, for example, overrides lookup.

Fix the calculation by putting braces around the value in the
subsctuction expression.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-12-07 13:08:23 -08:00
Michal Suchanek e22e1c1f6e depmod: Don't add .TOC. when it's in the kernel.
d46136bb59 ("depmod: Ignore PowerPC64 ABIv2 .TOC. symbol") adds fake
.TOC. unconditionally but when there is .TOC. in the kernel adding the
fake one breaks resolving .TOC.

Fixes: d46136bb59 ("depmod: Ignore PowerPC64 ABIv2 .TOC. symbol")

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
2017-12-07 11:04:37 -08:00
Yauheni Kaliuta 7da6884e73 depmod: implement external directories support
The idea is to add a configuration keyword, external, which
will list directories for scanning for particular kernel version
mask:

external 4.10 /the/modules/dir /second/modules/dir

And extend "search" keyword to set it's priority with pseudo dir
"external" (as it's done for built-in):

search subdir external subdir2 built-in subdir3

(actually, the version is the same as for override keyword: * or
posix regexp, so example above is a bit incorrect).

All other logic left the same: if there are duplicates, only one
is under consideration and it is unloadable if it is bad.

The resulting modules.dep will contain entries a-la:

/the/modules/dir/module1.ko:
kernel/module2.ko: /the/modules/dir/module1.ko

(here /lib/modules/$(uname -r)/kernel/module2.ko depends of
symbols, provided by /the/modules/dir/module1.ko and external has
higher priority).

modprobe and modinfo understand it out of box.

This is a pretty simple extention of existing logic, since now
depmod already is able to:

a) scan modules with full path from command line without -a
switch;
b) detects broken symbol dependencies and broken modversions,
what assumes, that modules are already are not built for the
existing kernel.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-06-01 20:30:03 -07:00
Yauheni Kaliuta 1399c5ad53 depmod: rewrite depmod modules search with scratchbuf
The recursive search code used used pretty big, PATH_MAX,
automatic storage buffer for the module directory scanning. Some
time ago there was scratchbuf implemented, which dynamically
reallocates its buffer on demand. The patch takes it in use for
the scanning code also. The initial size is hardcoded to 256
bytes which sounds good enough for most usecases so there should
be not many reallocations.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-06-01 20:23:13 -07:00
Yauheni Kaliuta 369ca65620 depmod: create depmod dir independent search function
Prepare to implement external directories support.

The patch splits depmod_modules_search() function to two
functions: depmod_modules_search(), called by the high level with
intention to search all possible modules, and
depmod_module_search_path(), which takes path as a parameter and
scans modules under the path only. Initially it is used to scan
the same depmod->cfg->dirname path only.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-06-01 19:17:42 -07:00
Yauheni Kaliuta 0d6b3f9bea depmod: search key: move builtin detection under the add function
Prepare to implement external directories support.

It's better to isolate behaviour difference under the
cfg_search_add() call, then make the client code aware of it.

In case of external modules/directories support, there will be
one more keyword added, so making the clients aware of it makes
even less sense.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-05-09 00:01:50 -07:00
Yauheni Kaliuta cd019a39ea depmod: fix errorpath memleaks in report cycles logic
The c7ce9f0c80 commit (depmod:
handle nested loops) introduced a bunch of possible memory leaks
in error path. In the real world scenario it is not a problem,
since the utility quits if it detects any of the errors, but from
the programming point of view, it is not nice. So, add the
cleanups.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-05-08 23:45:34 -07:00
Lucas De Marchi 3200780813 depmod: fix leak on error path 2017-02-23 23:18:02 -08:00
Yauheni Kaliuta c7ce9f0c80 depmod: handle nested loops
This is a rework of depmod report cycles logic to make it
tolerant to more complex loops.

The patch tries to remember own path for vertexes which makes it
possible to handle configurations with common edges and
non-cyclic modules.

It assumes that the previous dependency calculations can not give
as input something like

mod_a -> mod_b -> <loop>, but

<loop> -> mod_a -> mod_b should be fine.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-02-22 04:53:39 -08:00
Mian Yousaf Kaukab 6b77f18896 depmod: ignore related modules in depmod_report_cycles
Only print actual cyclic dependencies. Print count of all the modules
in cyclic dependency at the end of the function so that dependent
modules which are not in cyclic chain can be ignored.

Printing dependent modules which are not in cyclic chain causes buffer
overflow as m->modnamesz is not included in buffer size calculations
(loop == m is never true). This buffer overflow causes kmod to crash.

Update depmod test to reflect the change as well.

Reported-by: Andreas Färber <afaerber@suse.de>
Signed-off-by: Mian Yousaf Kaukab <yousaf.kaukab@suse.com>
2016-11-08 22:38:34 -02:00
Lucas De Marchi cb51a641d6 depmod: fix string overflow
Use scratchbuf to fix issue with strcpy that may overflow the buffer we
declared in the stack.
2016-08-15 10:26:42 -03:00
Anton Blanchard d46136bb59 depmod: Ignore PowerPC64 ABIv2 .TOC. symbol
The .TOC. symbol on the PowerPC64 ABIv2 identifies the GOT
pointer, similar to how other architectures use _GLOBAL_OFFSET_TABLE_.

This is not a symbol that needs relocation, and should be ignored
by depmod.
2016-06-11 00:27:39 -03:00
Josh Triplett 4c30a11d5f depmod: Don't insert comment in modules.devname if otherwise empty
This allows tools to detect the file as empty, such as via systemd's
ConditionFileNotEmpty.
2016-01-11 10:25:31 -02:00
Laura Abbott f3f62f5ec3 depmod: Don't fall back to uname on bad version
Currently, if a value that doesn't match a kernel version
("%u.%u") is passed in, depmod silently falls back to
using uname. Rather than try and work around the caller passing
bad data, just exit out instead.
2015-09-30 15:17:05 -03:00
Laura Abbott 50f43ce2f6 depmod: Remove unprinted debug messages
In between the start of the program and the call to log_setup_kmod_log,
the only messages that will be printed are the ones at or above the
global default level. Debug messages in this range will never be printed
so remove them.
2015-09-30 15:17:03 -03:00
Lucas De Marchi 655de2751d tools: display features in --version
Reviewed-by: Andreas Mohr <andim2@users.sf.net>
2015-06-08 22:38:02 -03:00
Lucas De Marchi 22df456760 depmod: add asserts to ensure positive return from ftell()
Also ignore some errors that will later be returned by ferror().
2015-02-28 14:57:00 -03:00
Lucas De Marchi a1bec0dfdb depmod: fix leak in case of malloc(0)
malloc(0) can return != NULL. We need to pass the pointer to free().
This happens if index__haschildren(node) returned true, but
child_count is set to 0.
2015-02-26 03:40:47 -03:00
Lucas De Marchi a07ea0329c depmod: use cleanup attribute to simplify free on exit
Reusing the root variable was a bad idea. Doing so we could call free()
on a variable that was not allocated. For example: "depmod -b / -h".
Since we would jump to cmdline_failed, root would not be duplicated.

Instead of fighting the order in the options, just used the cleanup
attribute and remove the calls to free() on "config_paths" and "root".
2015-02-25 12:06:44 -03:00
Colin Walters 0b3aef23b8 depmod: Fix crash in previous commit if root is not set
[This fixes http://build.gnome.org/continuous/buildmaster/builds/2015/02/25/31/build/output.txt ]

The variable we're reading here is "root", not "optarg" which is only
valid inside the getopt call.
2015-02-25 11:27:05 -03:00
Lucas De Marchi e90f0704ea depmod: fix leaking root on exit 2015-02-25 00:50:37 -03:00
Lucas De Marchi f357866d97 Fix includes after change to build-sys
Make the includes be libkmod/libkmod.h for code outside of library. This
fixes the broken build after 1315123 ('build-sys: Don't add libkmod
subdirectory to include path').
2015-01-02 12:41:01 -02:00
Lucas De Marchi 778395e4e8 depmod: point to documentation in libkmod
Instead of repeating all documentation, point to the documentation
available in libkmod-index.c

This also removes INDEX_PRIORITY_MIN that was not being used.
2014-11-15 11:18:46 -02:00
Lucas De Marchi f4e8c16291 Move remaining functions from libkmod-util to shared 2014-10-09 01:26:39 -03:00
Lucas De Marchi 3753ae16f5 depmod: use alias_normalize() from shared
Remove underscores2() function which is essentially the same as
alias_normalize() and the latter, which is now in shared/.
2014-10-09 01:26:39 -03:00
Lucas De Marchi b95506ff61 Do not export array of kmod extensions
The only user outside of libkmod-util is depmod, which really only needs
to get the string for the extension of uncompressed modules. It doesn't
need to access the array itself.
2014-10-09 01:26:39 -03:00
Lucas De Marchi aafd38359a Rename getline_wrapped() to freadline_wrapped() 2014-10-03 03:25:06 -03:00
Lucas De Marchi c2e4286bb9 Reorder and reorganize header files
Let the includes in the following order:

< system headers >
< libkmod >
< tool >
< local headers >
2014-10-03 01:43:15 -03:00
Lucas De Marchi 0db718edcf Move hash implementation to shared directory 2014-10-03 00:40:11 -03:00
Lucas De Marchi 74d1df6682 Move array implementation to shared directory 2014-10-03 00:33:25 -03:00
Lucas De Marchi 96573a0220 Move generic util functions to shared directory 2014-10-03 00:33:25 -03:00
Lucas De Marchi 576dd4393d Move macro.h to shared directory
It's not really related to libkmod, so move it to a directory in which
we keep common stuff.
2014-10-02 22:03:19 -03:00
Lucas De Marchi c89d219884 depmod: Add better error messages when facing loops
Since now depmod fails when there are module loops, let's at least give
better error messages, printing the loops we found. Since we may have
more than 1 loop, just printing the modules that are in loop is not
very clear.

Assuming as an example 2 independent loops, this is how the new messages
compare to the old ones:

Before:
	depmod: ERROR: Found 5 modules in dependency cycles!
	depmod: ERROR: /tmp/test-kmod//lib/modules/3.14.4-1-ARCH/kernel/moduleE.ko in dependency cycle!
	depmod: ERROR: /tmp/test-kmod//lib/modules/3.14.4-1-ARCH/kernel/moduleB.ko in dependency cycle!
	depmod: ERROR: /tmp/test-kmod//lib/modules/3.14.4-1-ARCH/kernel/moduleC.ko in dependency cycle!
	depmod: ERROR: /tmp/test-kmod//lib/modules/3.14.4-1-ARCH/kernel/moduleD.ko in dependency cycle!
	depmod: ERROR: /tmp/test-kmod//lib/modules/3.14.4-1-ARCH/kernel/moduleA.ko in dependency cycle!

After:
	depmod: ERROR: Found 5 modules in dependency cycles!
	depmod: ERROR: Cycle detected: moduleE -> moduleD -> moduleE
	depmod: ERROR: Cycle detected: moduleB -> moduleC -> moduleA -> moduleB
2014-05-30 09:43:30 -03:00
Lucas De Marchi a873f2350f depmod: Rename variable to clarify its meaning
In mod->modnamelen we were actually including the '\0', i.e.
strlen(modname) + 1. So rename it to modnamesz and add a comment in
depmod_module_is_higher_priority() to notice why it's correct since the
new one is really using strlen(modname).
2014-05-30 09:03:33 -03:00
Lucas De Marchi c48b269d64 depmod: Make dependency loops be fatal
Since the beginning depmod just warned about dependency loops and upon
creation of modules.dep{,.bin} it skipped the modules that were part of
a loop. However just skipping the modules may come as a surprise to
kernel module developers: they will need to try to load the module (or
to pay attention to the log messages) to notice thavt the module has not
been put in the index. Also, differently from module-init-tools we were
not skipping modules that depend on modules with dependency loops,
leading to a segfault in depmod.

So this is a summary of the change in behavior with this patch:

Loop 1)
    A -> B -> C -
    ^           |
    '------------

    Before:
        depmod: WARNING: found 3 modules in dependency cycles!
        depmod: WARNING: /tmp/test-kmod/lib/modules/3.14.2-1-ARCH/kernel/moduleB.ko in dependency cycle!
        depmod: WARNING: /tmp/test-kmod/lib/modules/3.14.2-1-ARCH/kernel/moduleC.ko in dependency cycle!
        depmod: WARNING: /tmp/test-kmod/lib/modules/3.14.2-1-ARCH/kernel/moduleA.ko in dependency cycle!

        return code: 0

    After:
        depmod: ERROR: Found 3 modules in dependency cycles!
        depmod: ERROR: /tmp/test-kmod/lib/modules/3.14.2-1-ARCH/kernel/moduleB.ko in dependency cycle!
        depmod: ERROR: /tmp/test-kmod/lib/modules/3.14.2-1-ARCH/kernel/moduleC.ko in dependency cycle!
        depmod: ERROR: /tmp/test-kmod/lib/modules/3.14.2-1-ARCH/kernel/moduleA.ko in dependency cycle!

        return code: 2

Loop 2)
    A -> B -> C -
         ^      |
         '-------

    Before:
        depmod: WARNING: found 2 modules in dependency cycles!
        depmod: WARNING: /tmp/test-kmod//lib/modules/3.14.2-1-ARCH/kernel/moduleB.ko in dependency cycle!
        depmod: WARNING: /tmp/test-kmod//lib/modules/3.14.2-1-ARCH/kernel/moduleC.ko in dependency cycle!
        Segmentation fault (core dumped)

    After:
        depmod: ERROR: Found 2 modules in dependency cycles!
        depmod: ERROR: /tmp/test-kmod/lib/modules/3.14.2-1-ARCH/kernel/moduleB.ko in dependency cycle!
        depmod: ERROR: /tmp/test-kmod/lib/modules/3.14.2-1-ARCH/kernel/moduleC.ko in dependency cycle!

        return code: 2

The segfault above could be fixed, but let's just fail everything
because dependency cycles should be fixed in the modules rather than
just be skipped in the index.
2014-05-14 20:05:12 -03:00
Tom Gundersen 8240333b25 config: also parse softdeps from modules
This information can be found in /lib/modules/`uname -r`/modules.softdep, and
has only recently been exported by the kernel.

Also remove the advice about copying modules.softdep to /lib/modules as it is
not clear how to do this correctly with several kernels installed with
potentially conflicting soft dependencies.
2014-04-01 08:13:54 -03:00
Anssi Hannula 49b33c1f21 depmod: do not allow partial matches with "search" directive
Currently e.g. "search foo foobar built-in" will cause unpredictable
results if baz.ko is in both foo/ and foobar/, since "foo" in search may
match both of those directories and the preferred module therefore
depends on processing order.

Fix the code to ensure that the match is performed on full pathname
components only.
2014-03-19 09:18:39 -03:00
Anssi Hannula 27881f6fbc depmod: fix debug print parameter order 2014-03-19 07:04:24 -03:00