If module cannot be removed in time, return with a failure, not success:
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/377
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
--remove-holders was not implying --remove, which means that if the user
called `modprobe --remove-holders xe` it would actually try to insert
the xe module. Fix it and spell it out in the man page about one option
implying the other.
Considering i915 is not currently loaded:
Before:
kmod $ ./build/modprobe -v --dry-run --remove-holders i915
insmod /lib/modules/6.14.11-1-MANJARO/kernel/drivers/char/agp/intel-gtt.ko.zst
insmod /lib/modules/6.14.11-1-MANJARO/kernel/drivers/gpu/drm/i915/i915.ko.zst
kmod $
After:
kmod $ ./build/modprobe -v --dry-run --remove-holders i915
kmod $
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/372
Use the newly introduced helper module_new_from_any(), so we accept both
files as well as modules. This means you can now do:
- modprobe --show-modversions drm-vram-helper, alongside the existing
- modprobe --show-modversions /full/path/to/the/module/drm_vram_helper.ko
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/353
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Use the newly introduced helper module_new_from_any(), so we accept both
files as well as modules. This means you can now do:
- modprobe --show-exports drm-vram-helper, alongside the existing
- modprobe --show-exports /full/path/to/the/module/drm_vram_helper.ko
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/353
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Split the functionality into a helper function, which will be used with
later commits.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/353
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Currently we have a range of options which show specific details,
although they vary in prefix or spelling:
- prefix: dump vs resolve vs show
- with and without dash after show
Converge on --show-foo and deprecate the rest. The old options are still
accepted but will throw a warning. Only the (new) consistent names are
listed in --help, while the manual page lists both but spells out that
the old ones will be removed.
To avoid any confusion with --show (aka --dry-run) the option is also
soft-deprecated.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/353
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The option was annotated as deprecated a while ago, although we never
flagged a warning when it is in use.
Do that, remove it from the --help screen and add an explicit "will be
removed" notice in the manual page.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/353
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The function returns the error code itself, so we should be using
strerror(-err) instead.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/368
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The KMOD API returns NULL on error and getting the exact means why it
failed is undefined. In practise the errno is set in _some_ of the error
paths, but not all.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/368
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The function returns the error code itself, so we should be using
strerror(-err) instead.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/368
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The function will always set errno of ENOMEM, so we might as well use
that. Making the code more consistent and removing a few bytes/LoC.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/368
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Currently our codebase has a mix of explicitly using ENOMEM and
propagating the errno... The latter of which is guaranteed to be ENOMEM
as documented in the manual pages.
Consolidate on ENOMEM, both for consistency sake and to remove a few
bytes off our binaries ;-)
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/368
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The argument was unused ever since the function was introduced - remove
it.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/361
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The function signature for our printers is uniform, even though in one
of the instances a parameter is unused. There is not much we can do
here, just silence the compiler warning.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/361
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Remove the initialization for interval_msec and tend_msec. They are set
on the first iteration... Assuming the user has set non-zero wait time.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/361
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
kmod_help() does not use neither argc nor argv although due to the
dispatch mechanism they are part of the function signature.
Not much we can do here - silence the compiler warnings.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/361
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
In a handful of functions we don't need the depmod struct. Remove it,
making the compiler happy and shaving a few bytes off the resulting
binary.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/361
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Current code uses the POSIX.1-2001 stat::st_mtime which has one second
granularity. Considering the depmod execution time may be smaller, we
should be using the POSIX.1-2008 stat::st_mtim which has nano-second
granularity instead.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/359
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The strto* family of functions may fail if the input string contains
a number which is too large for the designated data type. In such
cases, errno is set to ERANGE. Check for this error condition and if
subsequent casts would truncate the value.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/357
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The spec dictates that static variables are initialized to 0/NULL.
Remove the unnecessary explicit initialization - on older compilers this
sheds a few bytes off the data section of respective binaries.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/358
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Annotate the arrays (not the data) as constant, so the compiler can
store it in the RO segment.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/358
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
No longer needed since the introduction of tmpfile-util.
Fixes: aae48bc9 ("depmod: add tmpfile-util to generate temporary file")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/358
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Older compilers had struggles expanding strlen(string-literal) to a
constant compile time expression. Thus our code-base used sizeof() - 1
instead.
This has been resolved for years, so let's use the correct function.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/354
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Use our handy macro(s) for clarity and consistency sake.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/354
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The single caller of add_param() does not need the struct param*, so we
might as well return the error code directly. As a result we don't
manually overwrite errno.
Reference: https://github.com/kmod-project/kmod/issues/244
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/346
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The manual page of strerror() outlines a number of caveats wrt its usage.
Swap for the GNU specific %m printf modifier (also supported on musl and
bionic), which side-steps the issues as much as possible.
Co-authored-by: Cristian Rodríguez <cristian@rodriguez.im>
[emil: split from larger patch, convert more instances to %m]
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/346
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Bunch of our codebase uses sizeof() while the rest hard-coded length for
the destination buffer. Converge on the former since it makes for more
obvious and less error prone code.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/345
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Supports --force-{vermagic,modversion} parameter like modprobe.
Link: https://github.com/kmod-project/kmod/pull/340
Signed-off-by: Rong Tao <rongtao@cestc.cn>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Argument --force-modversions should be --force-modversion
Link: https://github.com/kmod-project/kmod/pull/339
Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
errno may be long overwritten at this point and doesn't mean much on a
return from the cb(). Just replace with the same error we are output on
the log.
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/337
* we use `mkstemp` to create the temporary file since it's a general
function in linux system and O_TMPFILE is not supported on all linux
system.
* add `struct tmpfile` to keep the `dirfd`, temp file `fd` and the
filename.
Co-developed-by: Wenjie Wang <wenjie2.wang@intel.com>
Signed-off-by: Wenjie Wang <wenjie2.wang@intel.com>
Co-developed-by: Gongjun Song <gongjun.song@intel.com>
Signed-off-by: Gongjun Song <gongjun.song@intel.com>
Signed-off-by: Dan He <dan.h.he@intel.com>
Signed-off-by: Qingqing Li <qingqing.li@intel.com>
Signed-off-by: Yuchi Chen <yuchi.chen@intel.com>
Link: https://github.com/kmod-project/kmod/pull/305
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
I don't think this was used before (at least I always build from the git
rootdir), and now that autotools is removed, they are wrong.
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/334
Drop the brackets to stay consistent with quoting of other arguments -
see --config and friends further down.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/332
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
a6f9cd0 ("tools/rmmod: consistently use ERR logging facility") fixed
the split between syslog and stderr of various error message substrings
by calling the ERR macro instead of writing directly to stderr, but in
doing so also completely mangled the output because the ERR macro
decorates its arguments:
$ rmmod iwlwifi
rmmod: ERROR: Module iwlwifi is in use by:rmmod: ERROR: iwlmvmrmmod: ERROR:
And in syslog:
$ rmmod -s iwlwifi
2025-03-24T17:22:34.878318+01:00 mangolassi rmmod: ERROR: Module iwlwifi is in use by:
2025-03-24T17:22:34.889145+01:00 mangolassi rmmod: ERROR: iwlmvm
2025-03-24T17:22:34.889224+01:00 mangolassi rmmod: ERROR:
This commit fixes that by building the holder names list with a strbuf
and then passes the whole thing at once to ERR.
Fixes: a6f9cd0 ("tools/rmmod: consistently use ERR logging facility")
Signed-off-by: Yannick Le Pennec <yannick.lepennec@live.fr>
Link: https://lore.kernel.org/r/VI1PR02MB48291E3BDB7E764ED67154949BA42@VI1PR02MB4829.eurprd02.prod.outlook.com
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/328
The getopt_long() API handles both space and = separated long options
just fine. On the other hand, supporting both in the shell completions
means that we'll have to quadruple them in terms of size/complexity.
So let's try to keep things simple and consistent, by recommending only
the space variant.
Make sure to update both the tools' --help and the respective man pages.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/313
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The strbuf_reserve_extra call is only left for a possible '/' addition,
which only occurs before depmod_modules_search_dir recursively calls
itself.
By adding the slash at the start, it also simplifies
depmod_modules_search_path which does not have to do this before calling
depmod_modules_search_dir on its own.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/296
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The function depmod_modules_search_dir is supposed to ignore all errors
encountered during directory entry iteration. This was not true for
out of memory conditions, which could also lead to wrong output.
Clarify this fact by turning it void and adjust callers accordingly.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/296
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The strbuf functionality is always used to create C strings, so already
reserve an extra byte for NUL whenever size allocations occur.
Together with the fact that strbuf_str already returns a const char *,
an empty strbuf may return an unmodifiable empty string.
This renders all strbuf_str return value checks obsolete.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/296
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The deps bin output uses a strbuf. A size of 2048 covers all cases
on current Arch Linux (max used size is 1548) and removes heap
allocations.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/296
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Clarify that the code does not perform error checks, for which we
regularly use checks and continue at the start, but merely checks
how to print/filter the data.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/290
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Remove the explicit basename(argv[0]) calls because we already
rely on program_invocation_short_name even before reaching
these lines. Unifies code and slightly reduces binary size.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/284
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The write_* functions have different return statement handling. Unify
them by removing "else" if the if-block itself returns and also unify
the error handling by checking for the error case, leaving the success
return statement at the end of the function.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/285
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Add = to modprobe help output for --wait to comply with all other long
option descriptions.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/287
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
If no -o option is given, use stdout directly without opening
/dev/stdout manually. In a chroot environment, this could lead
to /dev/stdout creation as a regular file.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/283
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Add a missing newline between two functions.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/282
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The use_syslog variable is only used within main function, so reduce
its visibility.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/282
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Since log_open takes a booln as argument, turn use_syslog into a
bool instead of using an int. Shrinks binary size with GCC and from
C point of view, this makes the code cleaner.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/282
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Using a CodeQL query, I discovered that the destination of a `sscanf` call could overflow.
Thus, we bound the buffer size to be PATH_MAX, to ensure that it is
not larger than `modname` or `devname`.
Signed-off-by: Grayson Nocera <gnocera@purdue.edu>
Suggested-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/260
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>