diff --git a/libmodprobe/include/modprobe/modprobe.h b/libmodprobe/include/modprobe/modprobe.h index 03fb0a3ac..913a7870b 100644 --- a/libmodprobe/include/modprobe/modprobe.h +++ b/libmodprobe/include/modprobe/modprobe.h @@ -29,6 +29,7 @@ class Modprobe { bool LoadWithAliases(const std::string& module_name, bool strict, const std::string& parameters = ""); bool Remove(const std::string& module_name); + std::vector ListModules(const std::string& pattern); void EnableBlacklist(bool enable); private: diff --git a/libmodprobe/libmodprobe.cpp b/libmodprobe/libmodprobe.cpp index 354ec7bce..010624240 100644 --- a/libmodprobe/libmodprobe.cpp +++ b/libmodprobe/libmodprobe.cpp @@ -363,3 +363,16 @@ bool Modprobe::Remove(const std::string& module_name) { } return true; } + +std::vector Modprobe::ListModules(const std::string& pattern) { + std::vector rv; + for (const auto& [module, deps] : module_deps_) { + // Attempt to match both the canonical module name and the module filename. + if (!fnmatch(pattern.c_str(), module.c_str(), 0)) { + rv.emplace_back(module); + } else if (!fnmatch(pattern.c_str(), basename(deps[0].c_str()), 0)) { + rv.emplace_back(deps[0]); + } + } + return rv; +}