From b0c4881ea924d21ca819b6cf999e97c3efd040b4 Mon Sep 17 00:00:00 2001 From: Steve Muckle Date: Fri, 29 May 2020 16:30:33 -0700 Subject: [PATCH] add libmodprobe api to query the number of modules loaded In the short term this will be used to help implement support for multiple kernel module directories but it may be useful in other contexts as well. Bug: 157645635 Change-Id: I15a252b6e9394292c8f3557ed65112c935830441 --- libmodprobe/include/modprobe/modprobe.h | 3 +++ libmodprobe/libmodprobe_ext.cpp | 1 + libmodprobe/libmodprobe_ext_test.cpp | 1 + libmodprobe/libmodprobe_test.cpp | 1 + 4 files changed, 6 insertions(+) diff --git a/libmodprobe/include/modprobe/modprobe.h b/libmodprobe/include/modprobe/modprobe.h index 297036e41..a7687af16 100644 --- a/libmodprobe/include/modprobe/modprobe.h +++ b/libmodprobe/include/modprobe/modprobe.h @@ -34,6 +34,8 @@ class Modprobe { bool GetAllDependencies(const std::string& module, std::vector* pre_dependencies, std::vector* dependencies, std::vector* post_dependencies); + void ResetModuleCount() { module_count_ = 0; } + int GetModuleCount() { return module_count_; } void EnableBlacklist(bool enable); void EnableVerbose(bool enable); @@ -65,5 +67,6 @@ class Modprobe { std::unordered_map module_options_; std::set module_blacklist_; std::unordered_set module_loaded_; + int module_count_ = 0; bool blacklist_enabled = false; }; diff --git a/libmodprobe/libmodprobe_ext.cpp b/libmodprobe/libmodprobe_ext.cpp index 99472c14d..658970874 100644 --- a/libmodprobe/libmodprobe_ext.cpp +++ b/libmodprobe/libmodprobe_ext.cpp @@ -63,6 +63,7 @@ bool Modprobe::Insmod(const std::string& path_name, const std::string& parameter LOG(INFO) << "Loaded kernel module " << path_name; module_loaded_.emplace(canonical_name); + module_count_++; return true; } diff --git a/libmodprobe/libmodprobe_ext_test.cpp b/libmodprobe/libmodprobe_ext_test.cpp index 057dea3a0..9ee5ba7ab 100644 --- a/libmodprobe/libmodprobe_ext_test.cpp +++ b/libmodprobe/libmodprobe_ext_test.cpp @@ -56,6 +56,7 @@ bool Modprobe::Insmod(const std::string& path_name, const std::string& parameter } modules_loaded.emplace_back(path_name + options); + module_count_++; return true; } diff --git a/libmodprobe/libmodprobe_test.cpp b/libmodprobe/libmodprobe_test.cpp index 879c7f2e9..eea0abd52 100644 --- a/libmodprobe/libmodprobe_test.cpp +++ b/libmodprobe/libmodprobe_test.cpp @@ -161,6 +161,7 @@ TEST(libmodprobe, Test) { EXPECT_TRUE(modules_loaded == expected_modules_loaded); + EXPECT_TRUE(m.GetModuleCount() == 15); EXPECT_TRUE(m.Remove("test4")); GTEST_LOG_(INFO) << "Expected modules loaded after removing test4 (in order):";