From nobody Wed May 14 10:43:19 2025 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 1524669693026865.8538387758886; Wed, 25 Apr 2018 08:21:33 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BD12930BEBC8; Wed, 25 Apr 2018 15:21:30 +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 863DF2015C00; Wed, 25 Apr 2018 15:21:29 +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 9543D4CAA4; Wed, 25 Apr 2018 15:21:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3PFG3NT020159 for ; Wed, 25 Apr 2018 11:16:03 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2D7412023239; Wed, 25 Apr 2018 15:16:03 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8A8682023430; Wed, 25 Apr 2018 15:16:02 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Wed, 25 Apr 2018 17:15:19 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: Kevin Wolf , Peter Krempa Subject: [libvirt] [PATCH 11/35] util: file: Add helper to determine whether a path is a CDROM 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.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Wed, 25 Apr 2018 15:21:32 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add detection mechanism which will allow to check whether a path to a block device is a physical CDROM drive. This will be useful once we will need to pass it to hypervisors. The linux implementation uses an ioctl to do the detection, while the fallback uses a simple string prefix match. Signed-off-by: Peter Krempa Reviewed-by: John Ferlan --- src/libvirt_private.syms | 1 + src/util/virfile.c | 56 ++++++++++++++++++++++++++++++++++++++++++++= +++- src/util/virfile.h | 2 ++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d2728749fb..4f911c10a8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1781,6 +1781,7 @@ virFileGetMountSubtree; virFileHasSuffix; virFileInData; virFileIsAbsPath; +virFileIsCdrom; virFileIsDir; virFileIsExecutable; virFileIsLink; diff --git a/src/util/virfile.c b/src/util/virfile.c index 24f866525f..ad59b7015b 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -59,8 +59,9 @@ # include # if HAVE_DECL_LO_FLAGS_AUTOCLEAR # include -# include # endif +# include +# include #endif #include "configmake.h" @@ -1938,6 +1939,59 @@ int virFileIsMountPoint(const char *file) } +#if defined(__linux__) +/** + * virFileIsCdrom: + * @filename: File to check + * + * Returns 1 if @filename is a cdrom device 0 if it is not a cdrom and -1 = on + * error. 'errno' of the failure is preserved and no libvirt errors are + * reported. + */ +int +virFileIsCdrom(const char *filename) +{ + struct stat st; + int fd; + int ret =3D -1; + + if ((fd =3D open(filename, O_RDONLY | O_NONBLOCK)) < 0) + goto cleanup; + + if (fstat(fd, &st) < 0) + goto cleanup; + + if (!S_ISBLK(st.st_mode)) { + ret =3D 0; + goto cleanup; + } + + /* Attempt to detect via a CDROM specific ioctl */ + if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) >=3D 0) + ret =3D 1; + else + ret =3D 0; + + cleanup: + VIR_FORCE_CLOSE(fd); + return ret; +} + +#else + +int +virFileIsCdrom(const char *filename) +{ + if (STRPREFIX(filename, "/dev/cd", NULL) || + STRPREFIX(filename, "/dev/acd", NULL)) + return 1; + + return 0; +} + +#endif /* defined(__linux__) */ + + #if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R static int virFileGetMountSubtreeImpl(const char *mtabpath, diff --git a/src/util/virfile.h b/src/util/virfile.h index cd2a3867c2..8ab7d2d6a6 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -207,6 +207,8 @@ enum { int virFileIsSharedFSType(const char *path, int fstypes) ATTRIBUTE_NONNULL= (1); int virFileIsSharedFS(const char *path) ATTRIBUTE_NONNULL(1); int virFileIsMountPoint(const char *file) ATTRIBUTE_NONNULL(1); +int virFileIsCdrom(const char *filename) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; int virFileGetMountSubtree(const char *mtabpath, const char *prefix, --=20 2.16.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list