[Toybox] [PATCH] modprobe: Make /proc/modules optional

Vincent Donnefort vdonnefort at google.com
Fri Dec 2 09:20:32 PST 2022


Currently, /proc/modules is a strong requirement for loading modules.
This is problematic as procfs might not always be available. Checking
/proc/modules only allows to flag if a module is already present to
avoid loading it. It's something the kernel handles well already
(finit_module returns EEXIST). So if this might be good to have, not
being able to check the module presence shouldn't prevent modprobe from
trying.

diff --git a/toys/pending/modprobe.c b/toys/pending/modprobe.c
index 45f8ea21..dd5f7726 100644
--- a/toys/pending/modprobe.c
+++ b/toys/pending/modprobe.c
@@ -489,15 +489,15 @@ void modprobe_main(void)
   }
 
   // Read /proc/modules to get loaded modules.
-  fs = xfopen("/proc/modules", "r");
-  
-  while (read_line(fs, &procline) > 0) {
+  fs = fopen("/proc/modules", "r");
+
+  while (fs && read_line(fs, &procline) > 0) {
     *strchr(procline, ' ') = 0;
     get_mod(procline, 1)->flags = MOD_ALOADED;
     free(procline);
     procline = NULL;
   }
-  fclose(fs);
+  if (fs) fclose(fs);
   if (FLAG(a) || FLAG(r)) for (; *argv; argv++) add_mod(*argv);
   else {
     add_mod(*argv);
-- 
2.39.0.rc0.267.gcb52ba06e7-goog



More information about the Toybox mailing list