From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: util-linux@vger.kernel.org
Cc: jkosina@suse.cz, kzak@redhat.com, kurt@garloff.de,
vsementsov@virtuozzo.com, den@openvz.org, msuchanek@suse.de,
efremov@linux.com
Subject: [PATCH 1/2] libblkid: introduce blkid_safe_open
Date: Thu, 9 Dec 2021 15:12:32 +0100 [thread overview]
Message-ID: <20211209141233.3774937-2-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20211209141233.3774937-1-vsementsov@virtuozzo.com>
Commit 39f5af25982d8b0244000e92a9d0e0e6557d0e17 modified open() calls
used to get fds for probing by adding O_NONBLOCK flag.
We want to modify this logic in the following commit. So, as a first
step create a generic wrapper on open, so blkid-related open logic live
in one place.
Formally blkid_safe_open() becomes recommended method to open device
file for probing, as it workarounds problems of opening CDROM (and in
further patch also of FLOPPY). So, it's good to make a public API
function.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
libblkid/src/blkid.h.in | 2 ++
libblkid/src/evaluate.c | 2 +-
libblkid/src/libblkid.sym | 4 ++++
libblkid/src/probe.c | 19 ++++++++++++++++++-
libblkid/src/verify.c | 2 +-
misc-utils/blkid.c | 2 +-
misc-utils/wipefs.c | 2 +-
7 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/libblkid/src/blkid.h.in b/libblkid/src/blkid.h.in
index 3cd4116d9..7ecf4f9be 100644
--- a/libblkid/src/blkid.h.in
+++ b/libblkid/src/blkid.h.in
@@ -217,6 +217,8 @@ extern char *blkid_evaluate_spec(const char *spec, blkid_cache *cache)
__ul_attribute__((warn_unused_result));
/* probe.c */
+extern int blkid_safe_open(const char *filename, int mode)
+ __ul_attribute__((warn_unused_result));
extern blkid_probe blkid_new_probe(void)
__ul_attribute__((warn_unused_result));
extern blkid_probe blkid_new_probe_from_filename(const char *filename)
diff --git a/libblkid/src/evaluate.c b/libblkid/src/evaluate.c
index 710eac956..1eefe455c 100644
--- a/libblkid/src/evaluate.c
+++ b/libblkid/src/evaluate.c
@@ -73,7 +73,7 @@ static int verify_tag(const char *devname, const char *name, const char *value)
blkid_probe_enable_partitions(pr, TRUE);
blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
- fd = open(devname, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
+ fd = blkid_safe_open(devname, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
errsv = errno;
goto done;
diff --git a/libblkid/src/libblkid.sym b/libblkid/src/libblkid.sym
index 366f2c0c0..1549a29f4 100644
--- a/libblkid/src/libblkid.sym
+++ b/libblkid/src/libblkid.sym
@@ -183,3 +183,7 @@ BLKID_2_37 {
blkid_probe_set_hint;
blkid_probe_reset_hints;
} BLKID_2_36;
+
+BLKID_2_38 {
+ blkid_safe_open;
+} BLKID_2_37;
diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
index 3685ea5e1..70e3dc0eb 100644
--- a/libblkid/src/probe.c
+++ b/libblkid/src/probe.c
@@ -189,6 +189,23 @@ blkid_probe blkid_clone_probe(blkid_probe parent)
}
+/**
+ * blkid_safe_open
+ * @filename: device or regular file
+ * @mode: open mode
+ *
+ * This wrapper is blkid-specific wrapper on open(). It's "safe" in a meaning
+ * that it doesn't change
+ *
+ * We add O_NONBLOCK flag to the mode, as opening CDROM without this flag may
+ * load to closing the rom (if it's open), which is bad thing in context of
+ * blkid: we don't want to change the actual device state.
+ */
+int blkid_safe_open(const char *filename, int mode)
+{
+ return open(filename, mode | O_NONBLOCK);
+}
+
/**
* blkid_new_probe_from_filename:
@@ -208,7 +225,7 @@ blkid_probe blkid_new_probe_from_filename(const char *filename)
int fd;
blkid_probe pr = NULL;
- fd = open(filename, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
+ fd = blkid_safe_open(filename, O_RDONLY|O_CLOEXEC);
if (fd < 0)
return NULL;
diff --git a/libblkid/src/verify.c b/libblkid/src/verify.c
index 3b9754f57..96b43634c 100644
--- a/libblkid/src/verify.c
+++ b/libblkid/src/verify.c
@@ -126,7 +126,7 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
}
}
- fd = open(dev->bid_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
+ fd = blkid_safe_open(dev->bid_name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
DBG(PROBE, ul_debug("blkid_verify: error %s (%d) while "
"opening %s", strerror(errno), errno,
diff --git a/misc-utils/blkid.c b/misc-utils/blkid.c
index 41826e6dc..1f7be80eb 100644
--- a/misc-utils/blkid.c
+++ b/misc-utils/blkid.c
@@ -516,7 +516,7 @@ static int lowprobe_device(blkid_probe pr, const char *devname,
int rc = 0;
static int first = 1;
- fd = open(devname, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
+ fd = blkid_safe_open(devname, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
warn(_("error: %s"), devname);
return BLKID_EXIT_NOTFOUND;
diff --git a/misc-utils/wipefs.c b/misc-utils/wipefs.c
index 78dc63ee7..2597135b5 100644
--- a/misc-utils/wipefs.c
+++ b/misc-utils/wipefs.c
@@ -390,7 +390,7 @@ new_probe(const char *devname, int mode)
return NULL;
if (mode) {
- int fd = open(devname, mode | O_NONBLOCK);
+ int fd = blkid_safe_open(devname, mode);
if (fd < 0)
goto error;
--
2.31.1
next prev parent reply other threads:[~2021-12-09 14:12 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-09 14:12 [PATCH 0/2] libblkid: don't use O_NONBLOCK for floppy Vladimir Sementsov-Ogievskiy
2021-12-09 14:12 ` Vladimir Sementsov-Ogievskiy [this message]
2021-12-09 14:12 ` [PATCH 2/2] libblkid: reopen floppy without O_NONBLOCK Vladimir Sementsov-Ogievskiy
2021-12-14 11:45 ` Michal Suchánek
2021-12-14 13:58 ` Vladimir Sementsov-Ogievskiy
2021-12-14 12:03 ` Karel Zak
2021-12-14 14:24 ` Vladimir Sementsov-Ogievskiy
2021-12-15 12:56 ` Karel Zak
2021-12-16 8:47 ` Vladimir Sementsov-Ogievskiy
2021-12-24 15:31 ` Vladimir Sementsov-Ogievskiy
2022-01-03 8:43 ` Karel Zak
2022-01-17 11:50 ` Karel Zak
2022-01-17 15:46 ` Vladimir Sementsov-Ogievskiy
2022-01-17 16:12 ` Karel Zak
2022-01-17 18:10 ` Vladimir Sementsov-Ogievskiy
2022-01-20 9:14 ` Karel Zak
2022-01-21 10:18 ` Karel Zak
2022-01-21 12:13 ` Vladimir Sementsov-Ogievskiy
2022-01-21 13:57 ` Karel Zak
2021-12-14 10:25 ` [PATCH 0/2] libblkid: don't use O_NONBLOCK for floppy Jiri Kosina
2021-12-14 10:29 ` Kurt Garloff
2021-12-14 11:46 ` Michal Suchánek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211209141233.3774937-2-vsementsov@virtuozzo.com \
--to=vsementsov@virtuozzo.com \
--cc=den@openvz.org \
--cc=efremov@linux.com \
--cc=jkosina@suse.cz \
--cc=kurt@garloff.de \
--cc=kzak@redhat.com \
--cc=msuchanek@suse.de \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox