From 0817efd73f04bf59d1234887bc3971d2d067067e Mon Sep 17 00:00:00 2001 From: Artem Blagodarenko Date: Fri, 9 Aug 2019 22:19:29 +0300 Subject: [PATCH] LU-12650 lib: fix strings comparison during mount searching get_root_path() returns path to "lustre" mount instead "lustre1" because last symbol is not taking in account during comparison. This bug has influence to get_root_path() users. For example, fid2path use get_root_path(). lfs path2fid /mnt/lustre2/foodir3 [0x200000401:0x1:0x0] lfs fid2path lustre2 [0x200000401:0x1:0x0] lfs fid2path: cannot find '[0x200000401:0x1:0x0]': No such file or directory umount /mnt/lustre lfs fid2path lustre2 [0x200000401:0x1:0x0] foodir3 This fix adds strings length comparison. Signed-off-by: Artem Blagodarenko Cray-bug-id: LUS-7693 Change-Id: I3275d2182486d25389814f4c25b3f2a54ec29469 Reviewed-on: https://review.whamcloud.com/35755 Tested-by: jenkins Reviewed-by: Andreas Dilger Tested-by: Maloo Reviewed-by: Alexander Zarochentsev --- lustre/utils/liblustreapi.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index 59d02de..d9962aa 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -1261,6 +1261,7 @@ int get_root_path(int want, char *fsname, int *outfd, char *path, int index) FILE *fp; int idx = 0, len = 0, mntlen, fd; int rc = -ENODEV; + int fsnamelen, mountlen; /* get the mount point */ fp = setmntent(PROC_MOUNTS, "r"); @@ -1294,18 +1295,20 @@ int get_root_path(int want, char *fsname, int *outfd, char *path, int index) ptr_end++; /* Check the fsname for a match, if given */ + mountlen = ptr_end - ptr; if (!(want & WANT_FSNAME) && fsname != NULL && - (strlen(fsname) > 0) && - (strncmp(ptr, fsname, ptr_end - ptr) != 0)) - continue; + (fsnamelen = strlen(fsname)) > 0 && + (fsnamelen != mountlen || + (strncmp(ptr, fsname, mountlen) != 0))) + continue; /* If the path isn't set return the first one we find */ if (path == NULL || strlen(path) == 0) { strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1); mntdir[sizeof(mntdir) - 1] = '\0'; if ((want & WANT_FSNAME) && fsname != NULL) { - strncpy(fsname, ptr, ptr_end - ptr); - fsname[ptr_end - ptr] = '\0'; + strncpy(fsname, ptr, mountlen); + fsname[mountlen] = '\0'; } rc = 0; break; @@ -1316,8 +1319,8 @@ int get_root_path(int want, char *fsname, int *outfd, char *path, int index) mntdir[sizeof(mntdir) - 1] = '\0'; len = mntlen; if ((want & WANT_FSNAME) && fsname != NULL) { - strncpy(fsname, ptr, ptr_end - ptr); - fsname[ptr_end - ptr] = '\0'; + strncpy(fsname, ptr, mountlen); + fsname[mountlen] = '\0'; } rc = 0; } -- 1.8.3.1