changeset 936:859a93658467

modinfo: support -b basedir and -k kernel.release, fix two bugs Add two less-frequently used flags for modinfo; -b specifies an alternate root and -k replaces the output of uname -r. Additionally, avoid a potential overflow in sprintf, and correct an inverted test.
author Isaac Dunham <idunham@lavabit.com>
date Fri, 28 Jun 2013 02:11:48 -0500
parents d952f44620d4
children 871c68a35e95
files toys/other/modinfo.c
diffstat 1 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/toys/other/modinfo.c	Wed Jun 26 23:22:43 2013 -0500
+++ b/toys/other/modinfo.c	Fri Jun 28 02:11:48 2013 -0500
@@ -2,13 +2,15 @@
  *
  * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
 
-USE_MODINFO(NEWTOY(modinfo, "<1F:0", TOYFLAG_BIN))
+USE_MODINFO(NEWTOY(modinfo, "<1b:k:F:0", TOYFLAG_BIN))
 
 config MODINFO
   bool "modinfo"
   default y
   help
-    usage: modinfo [-0] [-F field] [modulename...]
+    usage: modinfo [-0] [-b basedir] [-k kernrelease] [-F field] [modulename...]
+    Display module fields for all specified modules, looking in
+    <basedir>/lib/modules/<kernrelease>/ (kernrelease defaults to uname -r).
 */
 
 #define FOR_modinfo
@@ -16,6 +18,8 @@
 
 GLOBALS(
   char *field;
+  char *knam;
+  char *base;
 
   long mod;
 )
@@ -23,7 +27,7 @@
 static void output_field(char *field, char *value)
 {
   if (!TT.field) xprintf("%s:%*c", field, 15 - strlen(field), ' ');
-  else if (!strcmp(TT.field, field)) return;
+  else if (strcmp(TT.field, field)) return;
   xprintf("%s", value);
   xputc((toys.optflags & FLAG_0) ? 0 : '\n');
 }
@@ -100,7 +104,10 @@
       struct utsname uts;
 
       if (uname(&uts) < 0) perror_exit("bad uname");
-      sprintf(toybuf, "/lib/modules/%s", uts.release);
+      if (snprintf(toybuf, sizeof(toybuf), "%s/lib/modules/%s",
+          (toys.optflags & FLAG_b) ? TT.base : "",
+          (toys.optflags & FLAG_k) ? TT.knam : uts.release) >= sizeof(toybuf))
+            perror_exit("basedir/kernrelease too long");
       dirtree_read(toybuf, check_module);
     }
   }