From nobody Fri Apr 26 13:30:02 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 1539092938301268.08290768995744; Tue, 9 Oct 2018 06:48:58 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9143480F79; Tue, 9 Oct 2018 13:48:56 +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 5C5C86A96E; Tue, 9 Oct 2018 13:48:56 +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 076514BB79; Tue, 9 Oct 2018 13:48:56 +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 w99DmrCn007077 for ; Tue, 9 Oct 2018 09:48:54 -0400 Received: by smtp.corp.redhat.com (Postfix) id EEB671057063; Tue, 9 Oct 2018 13:48:53 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4AAC01057051; Tue, 9 Oct 2018 13:48:53 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 9 Oct 2018 15:48:44 +0200 Message-Id: <964d2ff1b4d14621f1149e3d6da0e22cbf8a97be.1539092795.git.mprivozn@redhat.com> In-Reply-To: References: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: hhan@redhat.com Subject: [libvirt] [PATCH 1/4] virfiletest: Fix test name prefix for virFileInData test 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 09 Oct 2018 13:48:57 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Because of lacking virTestCounterReset() call, the old test cases name was preserved. Signed-off-by: Michal Privoznik Reviewed-by: Jiri Denemark --- tests/virfiletest.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/virfiletest.c b/tests/virfiletest.c index efe15485eb..19dd06e49e 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -388,6 +388,7 @@ mymain(void) } while (0) =20 if (holesSupported()) { + virTestCounterReset("testFileInData "); DO_TEST_IN_DATA(true, 4, 4, 4); DO_TEST_IN_DATA(false, 4, 4, 4); DO_TEST_IN_DATA(true, 8, 8, 8); --=20 2.18.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri Apr 26 13:30:02 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 1539092942372708.1097446924331; Tue, 9 Oct 2018 06:49:02 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2CF2C9B42C; Tue, 9 Oct 2018 13:49:00 +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 D325D68891; Tue, 9 Oct 2018 13:48:59 +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 753EF4CAA0; Tue, 9 Oct 2018 13:48:59 +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 w99DmtrI007087 for ; Tue, 9 Oct 2018 09:48:55 -0400 Received: by smtp.corp.redhat.com (Postfix) id 247ED10840C1; Tue, 9 Oct 2018 13:48:55 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E8C0103BAB2; Tue, 9 Oct 2018 13:48:54 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 9 Oct 2018 15:48:45 +0200 Message-Id: <910730755e5e86a35a0b1a09cc2e84e1bf9c24e3.1539092795.git.mprivozn@redhat.com> In-Reply-To: References: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: hhan@redhat.com Subject: [libvirt] [PATCH 2/4] virfiletst: Test virFileIsSharedFS 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 09 Oct 2018 13:49:01 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Introduce some basic test cases for virFileIsSharedFS(). More will be added later. In order to achieve desired result, mocks for setmntent() and statfs() need to be invented because the first thing that virFileIsSharedFS() does is calling the latter. If it finds a FUSE mount it'll call the former. The mock might look a bit complicated, but in fact it's quite simple. The test sets LIBVIRT_MTAB env variable to hold the absolute path to a file containing mount table. Then, statfs() returns matching FS it finds, and setmntent() is there just to replace /proc/mounts with the file the test wants to load. Adding this test also exposed a bug we have - because we assume the given path points to a file we cut off what we assume is a file name to obtain directory path and only then we call statfs(). This is buggy because the passed path could be already a mount point. Signed-off-by: Michal Privoznik Reviewed-by: Jiri Denemark --- tests/Makefile.am | 7 +- tests/virfiledata/mounts3.txt | 33 ++++++++ tests/virfilemock.c | 154 ++++++++++++++++++++++++++++++++++ tests/virfiletest.c | 62 +++++++++++++- 4 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 tests/virfiledata/mounts3.txt create mode 100644 tests/virfilemock.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 51be1509e1..d7ec7e3a6f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -242,6 +242,7 @@ test_libraries +=3D virusbmock.la \ virnetdevbandwidthmock.la \ virnumamock.la \ virtestmock.la \ + virfilemock.la \ $(NULL) endif WITH_LINUX =20 @@ -1170,9 +1171,13 @@ virresctrltest_SOURCES =3D \ virresctrltest.c testutils.h testutils.c virfilewrapper.h virfilewrapper.c virresctrltest_LDADD =3D $(LDADDS) =20 +virfilemock_la_SOURCES =3D \ + virfilemock.c +virfilemock_la_LDFLAGS =3D $(MOCKLIBS_LDFLAGS) +virfilemock_la_LIBADD =3D $(MOCKLIBS_LIBS) else ! WITH_LINUX EXTRA_DIST +=3D vircaps2xmltest.c virnumamock.c virfilewrapper.c \ - virfilewrapper.h virresctrltest.c + virfilewrapper.h virresctrltest.c virfilemock.c endif ! WITH_LINUX =20 if WITH_NSS diff --git a/tests/virfiledata/mounts3.txt b/tests/virfiledata/mounts3.txt new file mode 100644 index 0000000000..226f67dc00 --- /dev/null +++ b/tests/virfiledata/mounts3.txt @@ -0,0 +1,33 @@ +/dev/root / xfs rw,noatime,attr2,inode64,noquota 0 0 +proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0 +tmpfs /run tmpfs rw,nodev,relatime,size=3D3281436k,mode=3D755 0 0 +sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0 +dev /dev devtmpfs rw,nosuid,relatime,size=3D10240k,nr_inodes=3D4093060,mod= e=3D755 0 0 +securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime= 0 0 +debugfs /sys/kernel/debug debugfs rw,nosuid,nodev,noexec,relatime 0 0 +mqueue /dev/mqueue mqueue rw,nosuid,nodev,noexec,relatime 0 0 +configfs /sys/kernel/config configfs rw,nosuid,nodev,noexec,relatime 0 0 +devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=3D5,mode=3D620,ptmxmo= de=3D000 0 0 +fusectl /sys/fs/fuse/connections fusectl rw,nosuid,nodev,noexec,relatime 0= 0 +shm /dev/shm tmpfs rw,nosuid,nodev,noexec,relatime 0 0 +cgroup_root /sys/fs/cgroup tmpfs rw,nosuid,nodev,noexec,relatime,size=3D10= 240k,mode=3D755 0 0 +openrc /sys/fs/cgroup/openrc cgroup rw,nosuid,nodev,noexec,relatime,releas= e_agent=3D/lib/rc/sh/cgroup-release-agent.sh,name=3Dopenrc 0 0 +none /sys/fs/cgroup/unified cgroup2 rw,nosuid,nodev,noexec,relatime,nsdele= gate 0 0 +cpuset /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset= 0 0 +cpu /sys/fs/cgroup/cpu cgroup rw,nosuid,nodev,noexec,relatime,cpu 0 0 +cpuacct /sys/fs/cgroup/cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpua= cct 0 0 +blkio /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0 +memory /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory= 0 0 +devices /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devi= ces 0 0 +freezer /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,free= zer 0 0 +net_cls /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_= cls 0 0 +perf_event /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatim= e,perf_event 0 0 +net_prio /sys/fs/cgroup/net_prio cgroup rw,nosuid,nodev,noexec,relatime,ne= t_prio 0 0 +hugetlb /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,huge= tlb 0 0 +pids /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0 +rdma /sys/fs/cgroup/rdma cgroup rw,nosuid,nodev,noexec,relatime,rdma 0 0 +binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,re= latime 0 0 +hugetlbfs /hugepages2M hugetlbfs rw,relatime,mode=3D1777,pagesize=3D2M 0 0 +none /run/user/1000 tmpfs rw,relatime,mode=3D700,uid=3D1000 0 0 +host:/nfs /nfs nfs4 rw,relatime,vers=3D4.1,rsize=3D1048576,wsize=3D1048576= ,namlen=3D255,hard,proto=3Dtcp6,timeo=3D600,retrans=3D2,sec=3Dsys,clientadd= r=3D::,local_lock=3Dnone,addr=3D:: 0 0 +dev /nfs/blah devtmpfs rw,nosuid,relatime,size=3D10240k,nr_inodes=3D409306= 0,mode=3D755 0 0 diff --git a/tests/virfilemock.c b/tests/virfilemock.c new file mode 100644 index 0000000000..d458616899 --- /dev/null +++ b/tests/virfilemock.c @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * Author: Michal Privoznik + */ + +#include + +#include +#include +#include +#if HAVE_LINUX_MAGIC_H +# include +#endif + +#include "virmock.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +static FILE *(*real_setmntent)(const char *filename, const char *type); +static int (*real_statfs)(const char *path, struct statfs *buf); + + +static void +init_syms(void) +{ + if (real_setmntent) + return; + + VIR_MOCK_REAL_INIT(setmntent); + VIR_MOCK_REAL_INIT(statfs); +} + + +FILE * +setmntent(const char *filename, const char *type) +{ + const char *mtab; + + init_syms(); + + if ((mtab =3D getenv("LIBVIRT_MTAB"))) + filename =3D mtab; + + return real_setmntent(filename, type); +} + + +#ifndef NFS_SUPER_MAGIC +# define NFS_SUPER_MAGIC 0x6969 +#endif +#ifndef OCFS2_SUPER_MAGIC +# define OCFS2_SUPER_MAGIC 0x7461636f +#endif +#ifndef GFS2_MAGIC +# define GFS2_MAGIC 0x01161970 +#endif +#ifndef AFS_FS_MAGIC +# define AFS_FS_MAGIC 0x6B414653 +#endif +#ifndef SMB_SUPER_MAGIC +# define SMB_SUPER_MAGIC 0x517B +#endif +#ifndef CIFS_SUPER_MAGIC +# define CIFS_SUPER_MAGIC 0xFF534D42 +#endif +#ifndef HUGETLBFS_MAGIC +# define HUGETLBFS_MAGIC 0x958458f6 +#endif +#ifndef FUSE_SUPER_MAGIC +# define FUSE_SUPER_MAGIC 0x65735546 +#endif + + +static int +statfs_mock(const char *mtab, + const char *path, + struct statfs *buf) +{ + FILE *f; + struct mntent mb; + char mntbuf[1024]; + int ret =3D -1; + + if (!(f =3D real_setmntent(mtab, "r"))) { + fprintf(stderr, "Unable to open %s", mtab); + abort(); + } + + while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { + int ftype; + + if (STRNEQ(mb.mnt_dir, path)) + continue; + + if (STREQ(mb.mnt_type, "nfs") || + STREQ(mb.mnt_type, "nfs4")) { + ftype =3D NFS_SUPER_MAGIC; + } else if (STREQ(mb.mnt_type, "gfs2")|| + STREQ(mb.mnt_type, "gfs2meta")) { + ftype =3D GFS2_MAGIC; + } else if (STREQ(mb.mnt_type, "ocfs2")) { + ftype =3D OCFS2_SUPER_MAGIC; + } else if (STREQ(mb.mnt_type, "afs")) { + ftype =3D AFS_FS_MAGIC; + } else if (STREQ(mb.mnt_type, "smb3")) { + ftype =3D SMB_SUPER_MAGIC; + } else if (STREQ(mb.mnt_type, "cifs")) { + ftype =3D CIFS_SUPER_MAGIC; + } else if (STRPREFIX(mb.mnt_type, "fuse")) { + ftype =3D FUSE_SUPER_MAGIC; + } else { + /* Everything else is EXT4. We don't care really for other pat= hs. */ + ftype =3D EXT4_SUPER_MAGIC; + } + + memset(buf, 0, sizeof(*buf)); + /* We only care about f_type so far. */ + buf->f_type =3D ftype; + ret =3D 0; + break; + } + + endmntent(f); + return ret; +} + + +int +statfs(const char *path, struct statfs *buf) +{ + const char *mtab; + + init_syms(); + + if ((mtab =3D getenv("LIBVIRT_MTAB"))) + return statfs_mock(mtab, path, buf); + + return real_statfs(path, buf); +} diff --git a/tests/virfiletest.c b/tests/virfiletest.c index 19dd06e49e..22c163f0a0 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -30,6 +30,7 @@ # include #endif =20 +#define VIR_FROM_THIS VIR_FROM_NONE =20 #if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R static int testFileCheckMounts(const char *prefix, @@ -309,6 +310,48 @@ testFileInData(const void *opaque) } =20 =20 +struct testFileIsSharedFSType { + const char *mtabFile; + const char *filename; + const bool expected; +}; + +static int +testFileIsSharedFSType(const void *opaque ATTRIBUTE_UNUSED) +{ +#ifndef __linux__ + return EXIT_AM_SKIP; +#else + const struct testFileIsSharedFSType *data =3D opaque; + char *mtabFile =3D NULL; + bool actual; + int ret =3D -1; + + if (virAsprintf(&mtabFile, abs_srcdir "/virfiledata/%s", data->mtabFil= e) < 0) + return -1; + + if (setenv("LIBVIRT_MTAB", mtabFile, 1) < 0) { + fprintf(stderr, "Unable to set env variable\n"); + goto cleanup; + } + + actual =3D virFileIsSharedFS(data->filename); + + if (actual !=3D data->expected) { + fprintf(stderr, "Unexpected FS type. Expected %d got %d\n", + data->expected, actual); + goto cleanup; + } + + ret =3D 0; + cleanup: + VIR_FREE(mtabFile); + unsetenv("LIBVIRT_MTAB"); + return ret; +#endif +} + + static int mymain(void) { @@ -396,7 +439,24 @@ mymain(void) DO_TEST_IN_DATA(true, 8, 16, 32, 64, 128, 256, 512); DO_TEST_IN_DATA(false, 8, 16, 32, 64, 128, 256, 512); } + +#define DO_TEST_FILE_IS_SHARED_FS_TYPE(mtab, file, exp) \ + do { \ + struct testFileIsSharedFSType data =3D { \ + .mtabFile =3D mtab, .filename =3D file, .expected =3D exp \ + }; \ + if (virTestRun(virTestCounterNext(), testFileIsSharedFSType, &data= ) < 0) \ + ret =3D -1; \ + } while (0) + + virTestCounterReset("testFileIsSharedFSType "); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts1.txt", "/boot/vmlinuz", false); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some= /file", false); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true); + /* TODO Detect bind mounts */ + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", true); + return ret !=3D 0 ? EXIT_FAILURE : EXIT_SUCCESS; } =20 -VIR_TEST_MAIN(mymain) +VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virfilemock.so") --=20 2.18.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri Apr 26 13:30:02 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 1539092943299251.93358478268112; Tue, 9 Oct 2018 06:49:03 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4BEE361476; Tue, 9 Oct 2018 13:49:01 +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 EF0E06F95C; Tue, 9 Oct 2018 13:49:00 +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 999F64CAAD; Tue, 9 Oct 2018 13:49:00 +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 w99Dmuei007095 for ; Tue, 9 Oct 2018 09:48:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id 212A81001F5E; Tue, 9 Oct 2018 13:48:56 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 763171062242; Tue, 9 Oct 2018 13:48:55 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 9 Oct 2018 15:48:46 +0200 Message-Id: <673f4713518a9b027bf4de0c130038ca1380735f.1539092795.git.mprivozn@redhat.com> In-Reply-To: References: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: hhan@redhat.com Subject: [libvirt] [PATCH 3/4] virFileIsSharedFSType: Detect direct mount points 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 09 Oct 2018 13:49:02 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" If the given path is already a mount point (e.g. a bind mount of a file, or simply a direct mount point of a FS), then our code fails to detect that because the first thing it does is cutting off part after last slash '/'. Signed-off-by: Michal Privoznik Reviewed-by: Jiri Denemark --- src/util/virfile.c | 8 ++++---- tests/virfiletest.c | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 2a7e87102a..666d703f99 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3534,15 +3534,16 @@ virFileIsSharedFSType(const char *path, int fstypes) { VIR_AUTOFREE(char *) dirpath =3D NULL; - char *p; + char *p =3D NULL; struct statfs sb; int statfs_ret; =20 if (VIR_STRDUP(dirpath, path) < 0) return -1; =20 - do { + statfs_ret =3D statfs(dirpath, &sb); =20 + while ((statfs_ret < 0) && (p !=3D dirpath)) { /* Try less and less of the path until we get to a * directory we can stat. Even if we don't have 'x' * permission on any directory in the path on the NFS @@ -3563,8 +3564,7 @@ virFileIsSharedFSType(const char *path, *p =3D '\0'; =20 statfs_ret =3D statfs(dirpath, &sb); - - } while ((statfs_ret < 0) && (p !=3D dirpath)); + } =20 if (statfs_ret < 0) { virReportSystemError(errno, diff --git a/tests/virfiletest.c b/tests/virfiletest.c index 22c163f0a0..42d918aecc 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -453,8 +453,7 @@ mymain(void) DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts1.txt", "/boot/vmlinuz", false); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some= /file", false); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true); - /* TODO Detect bind mounts */ - DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", true); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", false); =20 return ret !=3D 0 ? EXIT_FAILURE : EXIT_SUCCESS; } --=20 2.18.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri Apr 26 13:30:02 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 153909294846574.35306741195825; Tue, 9 Oct 2018 06:49:08 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9681030001DD; Tue, 9 Oct 2018 13:49:06 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 54DD417F60; Tue, 9 Oct 2018 13:49:06 +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 E16D618005B2; Tue, 9 Oct 2018 13:49:05 +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 w99Dmvi7007102 for ; Tue, 9 Oct 2018 09:48:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1D77E1001F5E; Tue, 9 Oct 2018 13:48:57 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7043F103BAB2; Tue, 9 Oct 2018 13:48:56 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 9 Oct 2018 15:48:47 +0200 Message-Id: <6fb19a29408c95c2e8c67b825261574cd1bff300.1539092795.git.mprivozn@redhat.com> In-Reply-To: References: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: hhan@redhat.com Subject: [libvirt] [PATCH 4/4] virfile: Rework 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.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Tue, 09 Oct 2018 13:49:07 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" There are couple of things wrong with the current implementation. The first one is that in the first loop the code tries to build a list of fuse.glusterfs mount points. Well, since the strings are allocated in a temporary buffer and are not duplicated this results in wrong decision made later in the code. The second problem is that the code does not take into account subtree mounts. For instance, if there's a fuse.gluster mounted at /some/path and another FS mounted at /some/path/subdir the code would not recognize this subdir mount. Reported-by: Han Han Signed-off-by: Michal Privoznik --- src/util/virfile.c | 58 +++++++++++++---------------------- tests/virfiledata/mounts3.txt | 2 ++ tests/virfiletest.c | 2 ++ 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 666d703f99..19f504cc6d 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3468,18 +3468,14 @@ static int virFileIsSharedFixFUSE(const char *path, long *f_type) { - char *dirpath =3D NULL; - const char **mounts =3D NULL; - size_t nmounts =3D 0; - char *p; FILE *f =3D NULL; struct mntent mb; char mntbuf[1024]; + char *mntDir =3D NULL; + char *mntType =3D NULL; + size_t maxMatching =3D 0; int ret =3D -1; =20 - if (VIR_STRDUP(dirpath, path) < 0) - return -1; - if (!(f =3D setmntent(PROC_MOUNTS, "r"))) { virReportSystemError(errno, _("Unable to open %s"), @@ -3488,43 +3484,33 @@ virFileIsSharedFixFUSE(const char *path, } =20 while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { - if (STRNEQ("fuse.glusterfs", mb.mnt_type)) + const char *p; + size_t len =3D strlen(mb.mnt_dir); + + if (!(p =3D STRSKIP(path, mb.mnt_dir))) continue; =20 - if (VIR_APPEND_ELEMENT_COPY(mounts, nmounts, mb.mnt_dir) < 0) - goto cleanup; - } - - /* Add NULL sentinel so that this is a virStringList */ - if (VIR_REALLOC_N(mounts, nmounts + 1) < 0) - goto cleanup; - mounts[nmounts] =3D NULL; - - do { - if ((p =3D strrchr(dirpath, '/')) =3D=3D NULL) { - virReportSystemError(EINVAL, - _("Invalid relative path '%s'"), path); - goto cleanup; + if (len > maxMatching) { + len =3D maxMatching; + VIR_FREE(mntType); + VIR_FREE(mntDir); + if (VIR_STRDUP(mntDir, mb.mnt_dir) < 0 || + VIR_STRDUP(mntType, mb.mnt_type) < 0) + goto cleanup; } + } =20 - if (p =3D=3D dirpath) - *(p+1) =3D '\0'; - else - *p =3D '\0'; - - if (virStringListHasString(mounts, dirpath)) { - VIR_DEBUG("Found gluster FUSE mountpoint=3D%s for path=3D%s. " - "Fixing shared FS type", dirpath, path); - *f_type =3D GFS2_MAGIC; - break; - } - } while (p !=3D dirpath); + if (STREQ_NULLABLE(mntType, "fuse.glusterfs")) { + VIR_DEBUG("Found gluster FUSE mountpoint=3D%s for path=3D%s. " + "Fixing shared FS type", mntDir, path); + *f_type =3D GFS2_MAGIC; + } =20 ret =3D 0; cleanup: + VIR_FREE(mntType); + VIR_FREE(mntDir); endmntent(f); - VIR_FREE(mounts); - VIR_FREE(dirpath); return ret; } =20 diff --git a/tests/virfiledata/mounts3.txt b/tests/virfiledata/mounts3.txt index 226f67dc00..134c6e8f81 100644 --- a/tests/virfiledata/mounts3.txt +++ b/tests/virfiledata/mounts3.txt @@ -31,3 +31,5 @@ hugetlbfs /hugepages2M hugetlbfs rw,relatime,mode=3D1777,= pagesize=3D2M 0 0 none /run/user/1000 tmpfs rw,relatime,mode=3D700,uid=3D1000 0 0 host:/nfs /nfs nfs4 rw,relatime,vers=3D4.1,rsize=3D1048576,wsize=3D1048576= ,namlen=3D255,hard,proto=3Dtcp6,timeo=3D600,retrans=3D2,sec=3Dsys,clientadd= r=3D::,local_lock=3Dnone,addr=3D:: 0 0 dev /nfs/blah devtmpfs rw,nosuid,relatime,size=3D10240k,nr_inodes=3D409306= 0,mode=3D755 0 0 +host:/gv0 /gluster fuse.glusterfs rw 0 0 +root@host:/tmp/mkdir /gluster/sshfs fuse.sshfs rw 0 0 diff --git a/tests/virfiletest.c b/tests/virfiletest.c index 42d918aecc..d5102b1cc4 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -454,6 +454,8 @@ mymain(void) DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some= /file", false); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true); 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); =20 return ret !=3D 0 ? EXIT_FAILURE : EXIT_SUCCESS; } --=20 2.18.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri Apr 26 13:30:02 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 1539182597907621.1261753472821; Wed, 10 Oct 2018 07:43:17 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 02CB6227D5; Wed, 10 Oct 2018 14:43:16 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AE65783EBB; Wed, 10 Oct 2018 14:43:15 +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 356C9180B5B7; Wed, 10 Oct 2018 14:43:13 +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 w9AEhBUg001621 for ; Wed, 10 Oct 2018 10:43:11 -0400 Received: by smtp.corp.redhat.com (Postfix) id 621B91057068; Wed, 10 Oct 2018 14:43:11 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id B13661001F58; Wed, 10 Oct 2018 14:43:07 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Wed, 10 Oct 2018 16:43:03 +0200 Message-Id: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: jdenemar@redhat.com Subject: [libvirt] [PATCH v2 4/4] virfile: Rework 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 10 Oct 2018 14:43:16 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" There are couple of things wrong with the current implementation. The first one is that in the first loop the code tries to build a list of fuse.glusterfs mount points. Well, since the strings are allocated in a temporary buffer and are not duplicated this results in wrong decision made later in the code. The second problem is that the code does not take into account subtree mounts. For instance, if there's a fuse.gluster mounted at /some/path and another FS mounted at /some/path/subdir the code would not recognize this subdir mount. Reported-by: Han Han Signed-off-by: Michal Privoznik Reviewed-by: Jiri Denemark --- diff to v1: - fixed while() loop - added more checks for @p - fixed @maxMatching assignment (facepalm) src/util/virfile.c | 59 ++++++++++++++--------------------- tests/virfiledata/mounts3.txt | 2 ++ tests/virfiletest.c | 2 ++ 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 666d703f99..f964806c3a 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3468,18 +3468,14 @@ static int virFileIsSharedFixFUSE(const char *path, long *f_type) { - char *dirpath =3D NULL; - const char **mounts =3D NULL; - size_t nmounts =3D 0; - char *p; FILE *f =3D NULL; struct mntent mb; char mntbuf[1024]; + char *mntDir =3D NULL; + char *mntType =3D NULL; + size_t maxMatching =3D 0; int ret =3D -1; =20 - if (VIR_STRDUP(dirpath, path) < 0) - return -1; - if (!(f =3D setmntent(PROC_MOUNTS, "r"))) { virReportSystemError(errno, _("Unable to open %s"), @@ -3488,43 +3484,36 @@ virFileIsSharedFixFUSE(const char *path, } =20 while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { - if (STRNEQ("fuse.glusterfs", mb.mnt_type)) - continue; + const char *p; + size_t len =3D strlen(mb.mnt_dir); =20 - if (VIR_APPEND_ELEMENT_COPY(mounts, nmounts, mb.mnt_dir) < 0) - goto cleanup; - } + if (!(p =3D STRSKIP(path, mb.mnt_dir))) + continue; =20 - /* Add NULL sentinel so that this is a virStringList */ - if (VIR_REALLOC_N(mounts, nmounts + 1) < 0) - goto cleanup; - mounts[nmounts] =3D NULL; + if (*(p - 1) !=3D '/' && *p !=3D '/' && *p !=3D '\0') + continue; =20 - do { - if ((p =3D strrchr(dirpath, '/')) =3D=3D NULL) { - virReportSystemError(EINVAL, - _("Invalid relative path '%s'"), path); - goto cleanup; + if (len > maxMatching) { + maxMatching =3D len; + VIR_FREE(mntType); + VIR_FREE(mntDir); + if (VIR_STRDUP(mntDir, mb.mnt_dir) < 0 || + VIR_STRDUP(mntType, mb.mnt_type) < 0) + goto cleanup; } + } =20 - if (p =3D=3D dirpath) - *(p+1) =3D '\0'; - else - *p =3D '\0'; - - if (virStringListHasString(mounts, dirpath)) { - VIR_DEBUG("Found gluster FUSE mountpoint=3D%s for path=3D%s. " - "Fixing shared FS type", dirpath, path); - *f_type =3D GFS2_MAGIC; - break; - } - } while (p !=3D dirpath); + if (STREQ_NULLABLE(mntType, "fuse.glusterfs")) { + VIR_DEBUG("Found gluster FUSE mountpoint=3D%s for path=3D%s. " + "Fixing shared FS type", mntDir, path); + *f_type =3D GFS2_MAGIC; + } =20 ret =3D 0; cleanup: + VIR_FREE(mntType); + VIR_FREE(mntDir); endmntent(f); - VIR_FREE(mounts); - VIR_FREE(dirpath); return ret; } =20 diff --git a/tests/virfiledata/mounts3.txt b/tests/virfiledata/mounts3.txt index 226f67dc00..134c6e8f81 100644 --- a/tests/virfiledata/mounts3.txt +++ b/tests/virfiledata/mounts3.txt @@ -31,3 +31,5 @@ hugetlbfs /hugepages2M hugetlbfs rw,relatime,mode=3D1777,= pagesize=3D2M 0 0 none /run/user/1000 tmpfs rw,relatime,mode=3D700,uid=3D1000 0 0 host:/nfs /nfs nfs4 rw,relatime,vers=3D4.1,rsize=3D1048576,wsize=3D1048576= ,namlen=3D255,hard,proto=3Dtcp6,timeo=3D600,retrans=3D2,sec=3Dsys,clientadd= r=3D::,local_lock=3Dnone,addr=3D:: 0 0 dev /nfs/blah devtmpfs rw,nosuid,relatime,size=3D10240k,nr_inodes=3D409306= 0,mode=3D755 0 0 +host:/gv0 /gluster fuse.glusterfs rw 0 0 +root@host:/tmp/mkdir /gluster/sshfs fuse.sshfs rw 0 0 diff --git a/tests/virfiletest.c b/tests/virfiletest.c index 42d918aecc..d5102b1cc4 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -454,6 +454,8 @@ mymain(void) DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some= /file", false); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true); 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); =20 return ret !=3D 0 ? EXIT_FAILURE : EXIT_SUCCESS; } --=20 2.18.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list