ueventd: Add dynamic kernel module loading
For some platforms it is not known at build time what devices
will be attached at runtime. Building into the kernel or pre-loading
at init all the modules that could be needed would unnecessary bloat
the kernel. The solution is dynamic kernel module loading.
The kernel will generate uevents when devices are added, userspace
should monitor for these events and load the compatible modules.
The init process already monitors for uevents, add here the ability
to respond to modalias events and preform the correct action.
Adding this to init is preferred over an external program as we
can read and process the module alias and dependency files once,
instead of for each module needing to be loaded.
Test: Run on Beagle-X15, check all needed modules are loaded (lsmod)
Change-Id: I1b57d9aeb0a9770f309207183dc4bc2b7b905f14
Signed-off-by: Andrew F. Davis <afd@ti.com>
Reviewed-by: Sam Protsenko <semen.protsenko@linaro.org>
diff --git a/init/ueventd.cpp b/init/ueventd.cpp
index cd45a3f..e9d829b 100644
--- a/init/ueventd.cpp
+++ b/init/ueventd.cpp
@@ -36,6 +36,7 @@
#include "devices.h"
#include "firmware_handler.h"
+#include "modalias_handler.h"
#include "selinux.h"
#include "uevent_listener.h"
#include "ueventd_parser.h"
@@ -106,9 +107,11 @@
class ColdBoot {
public:
- ColdBoot(UeventListener& uevent_listener, DeviceHandler& device_handler)
+ ColdBoot(UeventListener& uevent_listener, DeviceHandler& device_handler,
+ ModaliasHandler& modalias_handler)
: uevent_listener_(uevent_listener),
device_handler_(device_handler),
+ modalias_handler_(modalias_handler),
num_handler_subprocesses_(std::thread::hardware_concurrency() ?: 4) {}
void Run();
@@ -122,6 +125,7 @@
UeventListener& uevent_listener_;
DeviceHandler& device_handler_;
+ ModaliasHandler& modalias_handler_;
unsigned int num_handler_subprocesses_;
std::vector<Uevent> uevent_queue_;
@@ -133,6 +137,7 @@
for (unsigned int i = process_num; i < uevent_queue_.size(); i += total_processes) {
auto& uevent = uevent_queue_[i];
device_handler_.HandleDeviceEvent(uevent);
+ modalias_handler_.HandleModaliasEvent(uevent);
}
_exit(EXIT_SUCCESS);
}
@@ -230,6 +235,7 @@
SelabelInitialize();
DeviceHandler device_handler;
+ ModaliasHandler modalias_handler;
UeventListener uevent_listener;
{
@@ -251,7 +257,7 @@
}
if (access(COLDBOOT_DONE, F_OK) != 0) {
- ColdBoot cold_boot(uevent_listener, device_handler);
+ ColdBoot cold_boot(uevent_listener, device_handler, modalias_handler);
cold_boot.Run();
}
@@ -262,8 +268,9 @@
while (waitpid(-1, nullptr, WNOHANG) > 0) {
}
- uevent_listener.Poll([&device_handler](const Uevent& uevent) {
+ uevent_listener.Poll([&device_handler, &modalias_handler](const Uevent& uevent) {
HandleFirmwareEvent(uevent);
+ modalias_handler.HandleModaliasEvent(uevent);
device_handler.HandleDeviceEvent(uevent);
return ListenerAction::kContinue;
});