Use /sys/module instead of /proc/modules.

When libzfs checks if the module is loaded or not, it currently reads
/proc/modules and searches for a line matching the module name.

Unfortunately, if the module is included in the kernel itself (built-in
module), then /proc/modules won't list it, so libzfs will wrongly conclude
that the module is not loaded, thus making all ZFS userspace tools unusable.

Fortunately, all loaded modules appear as directories in /sys/module, even
built-in ones. Thus we can use /sys/module in lieu of /proc/modules to fix
the issue.

As a bonus, the code for checking becomes much simpler.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #851
This commit is contained in:
Etienne Dechamps 2012-07-04 15:56:40 +02:00 committed by Brian Behlendorf
parent 2ee4a18b2a
commit f09398cec6
1 changed files with 5 additions and 19 deletions

View File

@ -608,27 +608,13 @@ libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
static int static int
libzfs_module_loaded(const char *module) libzfs_module_loaded(const char *module)
{ {
FILE *f; const char path_prefix[] = "/sys/module/";
int result = 0; char path[256];
char name[256];
f = fopen("/proc/modules", "r"); memcpy(path, path_prefix, sizeof(path_prefix) - 1);
if (f == NULL) strcpy(path + sizeof(path_prefix) - 1, module);
return -1;
while (fgets(name, sizeof(name), f)) { return (access(path, F_OK) == 0);
char *c = strchr(name, ' ');
if (!c)
continue;
*c = 0;
if (strcmp(module, name) == 0) {
result = 1;
break;
}
}
fclose(f);
return result;
} }
int int