public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] fdisk: warn if opening a device in write mode failed
@ 2014-02-27  3:26 Maciej Małecki
  2014-02-27  3:26 ` [PATCH 2/2] fdisk: do not allow writing to a read-only device Maciej Małecki
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Maciej Małecki @ 2014-02-27  3:26 UTC (permalink / raw)
  To: util-linux; +Cc: Maciej Małecki

Otherwise, `fdisk` fails silently with "Bad file descriptor" when
writing the partition table.

Signed-off-by: Maciej Małecki <me@mmalecki.com>
---
 libfdisk/src/context.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index c405403..40f9080 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -248,17 +248,26 @@ static int warn_wipe(struct fdisk_context *cxt)
 int fdisk_context_assign_device(struct fdisk_context *cxt,
 				const char *fname, int readonly)
 {
-	int fd;
+	int fd, mode;
 
 	DBG(CONTEXT, dbgprint("assigning device %s", fname));
 	assert(cxt);
 
 	reset_context(cxt);
 
-	if (readonly == 1 || (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) {
-		if ((fd = open(fname, O_RDONLY|O_CLOEXEC)) < 0)
+retry:
+	mode = (readonly ? O_RDONLY : O_RDWR) | O_CLOEXEC;
+	fd = open(fname, mode);
+	if (fd < 0) {
+		if (readonly)
 			return -errno;
-		readonly = 1;
+		else {
+			fdisk_warn(cxt,
+			    _("%s: opening device in read write mode failed"),
+			    fname);
+			readonly = 1;
+			goto retry;
+		}
 	}
 
 	cxt->dev_fd = fd;
-- 
1.9.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2014-03-21 13:11 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-02-27  3:26 [PATCH 1/2] fdisk: warn if opening a device in write mode failed Maciej Małecki
2014-02-27  3:26 ` [PATCH 2/2] fdisk: do not allow writing to a read-only device Maciej Małecki
2014-02-28  2:32 ` [PATCH 1/2] fdisk: warn if opening a device in write mode failed Davidlohr Bueso
2014-02-28 11:00   ` Maciej Małecki
2014-02-28 16:23     ` Phillip Susi
2014-02-28 18:18       ` Maciej Małecki
2014-02-28 18:27       ` Davidlohr Bueso
2014-02-28 19:18         ` Phillip Susi
2014-03-03  9:04           ` Karel Zak
2014-03-03 21:19             ` Maciej Małecki
2014-03-03 21:47               ` Phillip Susi
2014-03-21 13:10       ` Karel Zak
2014-03-03 21:14 ` [PATCH v2 1/3] fdisk: do not allow writing to a read-only device Maciej Małecki
2014-03-03 21:14   ` [PATCH v2 2/3] libfdisk: remove `O_RDONLY` fallback mode when opening device Maciej Małecki
2014-03-03 21:14   ` [PATCH v2 3/3] fdisk: retry opening device in read-only mode Maciej Małecki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox