From: Daniel Golle Date: Thu, 21 Jul 2016 01:55:48 +0000 (+0200) Subject: block: also check /proc/self/mountinfo to find mountpoint X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=bef7987f4b4ccc343f619172e28bcaaa12a19403;p=oweals%2Ffstools.git block: also check /proc/self/mountinfo to find mountpoint 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 --- diff --git a/block.c b/block.c index a06e200..58487a1 100644 --- 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; }