Skip to content

lsmod, libkmod: handle module changes during execution #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libkmod/libkmod-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -2049,7 +2049,7 @@ KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
fd = open(path, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
err = -errno;
DBG(mod->ctx, "could not open '%s': %s\n",
ERR(mod->ctx, "could not open '%s': %s\n",
path, strerror(errno));
return err;
}
Expand Down
12 changes: 10 additions & 2 deletions tools/lsmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,19 @@ static int do_lsmod(int argc, char *argv[])
const char *name = kmod_module_get_name(mod);
int use_count = kmod_module_get_refcnt(mod);
long size = kmod_module_get_size(mod);
struct kmod_list *holders, *hitr;
struct kmod_list *holders = kmod_module_get_holders(mod), *hitr;
int first = 1;

if (holders == NULL || use_count < 0 || size < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

holders == NULL is a normal thing to have, e.g:

serio_raw              20480  0

use_count < 0 allows us to use modules in the "going" state, i.e. in the process of being unloaded.

size < 0 I believe is the only one that we should really do something.... maybe setting it to 0. Rather than exiting here, I think we should just handle the races as a normal situation and continue printing the rest of the list.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An illustration of negative use_count:

In a handful of instances (buggy/glitchy bluetooth + rmmod non-force) I have ended up with foobar 1234 -1. At which point the kernel(?) was completely foobar'd and reboot was needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SuibianP can you rebase taking this into account?

fprintf(stderr, "Error: could not query module %s! Was the module unloaded?\n", name);
kmod_module_unref_list(holders);
kmod_module_unref(mod);
kmod_module_unref_list(list);
kmod_unref(ctx);
return EXIT_FAILURE;
}

printf("%-19s %8ld %d", name, size, use_count);
holders = kmod_module_get_holders(mod);
kmod_list_foreach(hitr, holders) {
struct kmod_module *hm = kmod_module_get_module(hitr);

Expand Down