block: also check /proc/self/mountinfo to find mountpoint
authorDaniel Golle <daniel@makrotopia.org>
Thu, 21 Jul 2016 01:55:48 +0000 (03:55 +0200)
committerDaniel Golle <daniel@makrotopia.org>
Thu, 21 Jul 2016 02:11:41 +0000 (04:11 +0200)
Matching only the device name doesn't always work, e.g. in case of
/dev/root. Thus also check the device(minor,major) which can be scraped
from /proc/self/mountinfo.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
block.c

diff --git a/block.c b/block.c
index a06e2009ff97dc220c0904c97debda05745f211f..58487a1a1935e0fe65f2c62e18e73b901feff294 100644 (file)
--- a/block.c
+++ b/block.c
@@ -588,9 +588,10 @@ static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char
 static char* find_mount_point(char *block)
 {
        FILE *fp = fopen("/proc/mounts", "r");
-       static char line[256];
+       static char line[256], *saveptr;
        int len = strlen(block);
        char *point = NULL;
+       struct stat s;
 
        if(!fp)
                return NULL;
@@ -612,6 +613,32 @@ static char* find_mount_point(char *block)
 
        fclose(fp);
 
+       if (point)
+               return point;
+
+       if (stat(block, &s))
+               return NULL;
+
+       if (!S_ISBLK(s.st_mode))
+               return NULL;
+
+       fp = fopen("/proc/self/mountinfo", "r");
+       if(!fp)
+               return NULL;
+
+       while (fgets(line, sizeof(line), fp)) {
+               strtok_r(line, " \t", &saveptr);
+               strtok_r(NULL, " \t", &saveptr);
+               if (atoi(strtok_r(NULL, ":", &saveptr)) == major(s.st_rdev) &&
+                   atoi(strtok_r(NULL, " \t", &saveptr)) == minor(s.st_rdev)) {
+                       strtok_r(NULL, " \t", &saveptr);
+                       point = strtok_r(NULL, " \t", &saveptr);
+                       break;
+               }
+       }
+
+       fclose(fp);
+
        return point;
 }