From nobody Fri Apr 26 23:32:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 153987563705143.12737704572771; Thu, 18 Oct 2018 08:13:57 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 38FC8552E9; Thu, 18 Oct 2018 15:13:55 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F170A85572; Thu, 18 Oct 2018 15:13:54 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 924384CA94; Thu, 18 Oct 2018 15:13:54 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w9IF4g8M017404 for ; Thu, 18 Oct 2018 11:04:42 -0400 Received: by smtp.corp.redhat.com (Postfix) id D49AE103BAB3; Thu, 18 Oct 2018 15:04:42 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5B9CE1001F59 for ; Thu, 18 Oct 2018 15:04:42 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Oct 2018 17:04:37 +0200 Message-Id: <83439a7ef28c86dd3756aaeea381cc0a949c144d.1539875077.git.mprivozn@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] virfile: Take symlink into account in virFileIsSharedFixFUSE X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 18 Oct 2018 15:13:55 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" https://bugzilla.redhat.com/show_bug.cgi?id=3D1640465 Weirdly enough, there can be symlinks in the path we are trying to fix. If it is the case our clever algorithm that finds matches against mount table won't work. Canonicalize path at the beginning then. Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety --- cfg.mk | 2 +- src/util/virfile.c | 17 ++++++++++++++--- tests/virfilemock.c | 33 ++++++++++++++++++++++++++++++++- tests/virfiletest.c | 1 + 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/cfg.mk b/cfg.mk index 4790d0b7e7..d0183c02ff 100644 --- a/cfg.mk +++ b/cfg.mk @@ -1229,7 +1229,7 @@ exclude_file_name_regexp--sc_prohibit_select =3D \ ^cfg\.mk$$ =20 exclude_file_name_regexp--sc_prohibit_canonicalize_file_name =3D \ - ^cfg\.mk$$ + ^(cfg\.mk|tests/virfilemock\.c)$$ =20 exclude_file_name_regexp--sc_prohibit_raw_allocation =3D \ ^(docs/hacking\.html\.in|src/util/viralloc\.[ch]|examples/.*|tests/(secu= rityselinuxhelper|(vircgroup|nss)mock|commandhelper)\.c|tools/wireshark/src= /packet-libvirt\.c)$$ diff --git a/src/util/virfile.c b/src/util/virfile.c index e09992e41a..4747a26ad3 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3473,9 +3473,19 @@ virFileIsSharedFixFUSE(const char *path, char mntbuf[1024]; char *mntDir =3D NULL; char *mntType =3D NULL; + char *canonPath =3D NULL; size_t maxMatching =3D 0; int ret =3D -1; =20 + if (!(canonPath =3D virFileCanonicalizePath(path))) { + virReportSystemError(errno, + _("unable to canonicalize %s"), + path); + return -1; + } + + VIR_DEBUG("Path %s after canonicalization %s", path, canonPath); + if (!(f =3D setmntent(PROC_MOUNTS, "r"))) { virReportSystemError(errno, _("Unable to open %s"), @@ -3487,7 +3497,7 @@ virFileIsSharedFixFUSE(const char *path, const char *p; size_t len =3D strlen(mb.mnt_dir); =20 - if (!(p =3D STRSKIP(path, mb.mnt_dir))) + if (!(p =3D STRSKIP(canonPath, mb.mnt_dir))) continue; =20 if (*(p - 1) !=3D '/' && *p !=3D '/' && *p !=3D '\0') @@ -3504,13 +3514,14 @@ virFileIsSharedFixFUSE(const char *path, } =20 if (STREQ_NULLABLE(mntType, "fuse.glusterfs")) { - VIR_DEBUG("Found gluster FUSE mountpoint=3D%s for path=3D%s. " - "Fixing shared FS type", mntDir, path); + VIR_DEBUG("Found gluster FUSE mountpoint=3D%s for canonPath=3D%s. " + "Fixing shared FS type", mntDir, canonPath); *f_type =3D GFS2_MAGIC; } =20 ret =3D 0; cleanup: + VIR_FREE(canonPath); VIR_FREE(mntType); VIR_FREE(mntDir); endmntent(f); diff --git a/tests/virfilemock.c b/tests/virfilemock.c index 822c757380..ae5c8d025a 100644 --- a/tests/virfilemock.c +++ b/tests/virfilemock.c @@ -28,11 +28,14 @@ #endif =20 #include "virmock.h" +#include "virstring.h" +#include "viralloc.h" =20 #define VIR_FROM_THIS VIR_FROM_NONE =20 static FILE *(*real_setmntent)(const char *filename, const char *type); static int (*real_statfs)(const char *path, struct statfs *buf); +static char *(*real_canonicalize_file_name)(const char *path); =20 =20 static void @@ -43,6 +46,7 @@ init_syms(void) =20 VIR_MOCK_REAL_INIT(setmntent); VIR_MOCK_REAL_INIT(statfs); + VIR_MOCK_REAL_INIT(canonicalize_file_name); } =20 =20 @@ -94,6 +98,7 @@ statfs_mock(const char *mtab, FILE *f; struct mntent mb; char mntbuf[1024]; + char *canonPath =3D NULL; int ret =3D -1; =20 if (!(f =3D real_setmntent(mtab, "r"))) { @@ -101,10 +106,16 @@ statfs_mock(const char *mtab, return -1; } =20 + /* We don't need to do this in callers because real statfs(2) + * does that for us. However, in mocked implementation we + * need to do this. */ + if (!(canonPath =3D canonicalize_file_name(path))) + return -1; + while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { int ftype; =20 - if (STRNEQ(mb.mnt_dir, path)) + if (STRNEQ(mb.mnt_dir, canonPath)) continue; =20 if (STREQ(mb.mnt_type, "nfs") || @@ -136,6 +147,7 @@ statfs_mock(const char *mtab, } =20 endmntent(f); + VIR_FREE(canonPath); return ret; } =20 @@ -152,3 +164,22 @@ statfs(const char *path, struct statfs *buf) =20 return real_statfs(path, buf); } + + +char * +canonicalize_file_name(const char *path) +{ + if (getenv("LIBVIRT_MTAB")) { + const char *p; + char *ret; + + if ((p =3D STRSKIP(path, "/some/symlink"))) + ignore_value(virAsprintfQuiet(&ret, "/gluster%s", p)); + else + ignore_value(VIR_STRDUP_QUIET(ret, path)); + + return ret; + } + + return real_canonicalize_file_name(path); +} diff --git a/tests/virfiletest.c b/tests/virfiletest.c index 1f2be74c8d..f7b263f2e9 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -456,6 +456,7 @@ mymain(void) DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", false); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gluster/file", true); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gluster/sshfs/file", f= alse); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/some/symlink/file", tr= ue); =20 return ret !=3D 0 ? EXIT_FAILURE : EXIT_SUCCESS; } --=20 2.18.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list