ah, yeah, seq_put_hex_ll() takes a _minimum_ field width.

new patch attached that fixes that and adds a comment making the origin of
the 10 clearer. (both patches "work for me" since i don't actually have any
non-8-bit devices :-( )



On Sun, Sep 26, 2021 at 1:14 PM Rob Landley <r...@landley.net> wrote:

> On 9/25/21 8:55 PM, enh via Toybox wrote:
> > These are 2 hex digits each with a ':' between them in the kernel.
>
> Major has been 12 bits and minor 20 bits since 2003. (Git annotate on the
> header
> says https://github.com/mpe/linux-fullhistory/commit/7ae6a13832a2 .)
>
> So xxx:xxxxx, which is why I used a 10 byte buffer.
>
> Rob
>
From e6cc8e7bc864c2dad9b50965a107d06314903c3e Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Mon, 27 Sep 2021 10:15:58 -0700
Subject: [PATCH] lsof: explicit size for device major:minor buffer.

Major and minor get 12 bits and 20 bits respectively, so when displayed
that's up to 3 digits (but normally 2), a ':', and up to 5 digits (but
normally 2).
---
 toys/pending/lsof.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/toys/pending/lsof.c b/toys/pending/lsof.c
index 2ab4177a..e4b53112 100644
--- a/toys/pending/lsof.c
+++ b/toys/pending/lsof.c
@@ -330,10 +330,10 @@ static void visit_maps(struct proc_info *pi)
 {
   FILE *fp;
   unsigned long long offset;
-  char device[10];
   long inode;
-  char *line = NULL;
+  char *line = NULL, device[10]; // xxx:xxxxx\0
   size_t line_length = 0;
+  struct file_info *fi;
 
   snprintf(toybuf, sizeof(toybuf), "/proc/%d/maps", pi->pid);
   fp = fopen(toybuf, "r");
@@ -342,10 +342,8 @@ static void visit_maps(struct proc_info *pi)
   while (getline(&line, &line_length, fp) > 0) {
     int name_pos;
 
-    if (sscanf(line, "%*x-%*x %*s %llx %s %ld %n",
+    if (sscanf(line, "%*x-%*x %*s %llx %9s %ld %n",
                &offset, device, &inode, &name_pos) >= 3) {
-      struct file_info *fi;
-
       // Ignore non-file maps.
       if (inode == 0 || !strcmp(device, "00:00")) continue;
       // TODO: show unique maps even if they have a non-zero offset?
-- 
2.33.0.685.g46640cef36-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to