All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Maciej Małecki" <me@mmalecki.com>
To: util-linux@vger.kernel.org
Cc: "Maciej Małecki" <me@mmalecki.com>
Subject: [PATCH 2/2] fdisk: do not allow writing to a read-only device
Date: Thu, 27 Feb 2014 04:26:40 +0100	[thread overview]
Message-ID: <1393471600-17777-2-git-send-email-me@mmalecki.com> (raw)
In-Reply-To: <1393471600-17777-1-git-send-email-me@mmalecki.com>

Print a warning instead. This fixes the aforementioned "Bad file
descriptor" issue when writing to read-only completely.

Signed-off-by: Maciej Małecki <me@mmalecki.com>
---
 fdisks/fdisk-menu.c    | 4 ++++
 libfdisk/src/context.c | 3 +++
 libfdisk/src/fdiskP.h  | 1 +
 3 files changed, 8 insertions(+)

diff --git a/fdisks/fdisk-menu.c b/fdisks/fdisk-menu.c
index ced819a..f965e3f 100644
--- a/fdisks/fdisk-menu.c
+++ b/fdisks/fdisk-menu.c
@@ -448,6 +448,10 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
 		rc = fdisk_list_disklabel(cxt);
 		break;
 	case 'w':
+		if (cxt->readonly) {
+			fdisk_warnx(cxt, _("Device open in read-only mode"));
+			break;
+		}
 		rc = fdisk_write_disklabel(cxt);
 		if (rc)
 			err(EXIT_FAILURE, _("failed to write disklabel"));
diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index 40f9080..05833df 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -45,6 +45,7 @@ struct fdisk_context *fdisk_new_nested_context(struct fdisk_context *parent,
 	DBG(LABEL, dbgprint("new context %p allocated", cxt));
 	cxt->dev_fd = parent->dev_fd;
 	cxt->parent = parent;
+	cxt->readonly = parent->readonly;
 
 	cxt->io_size =          parent->io_size;
 	cxt->optimal_io_size =  parent->optimal_io_size;
@@ -173,6 +174,7 @@ static void reset_context(struct fdisk_context *cxt)
 
 	/* initialize */
 	cxt->dev_fd = -1;
+	cxt->readonly = -1;
 	cxt->dev_path = NULL;
 	cxt->firstsector = NULL;
 
@@ -271,6 +273,7 @@ retry:
 	}
 
 	cxt->dev_fd = fd;
+	cxt->readonly = readonly;
 	cxt->dev_path = strdup(fname);
 	if (!cxt->dev_path)
 		goto fail;
diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h
index feaab3d..da56c13 100644
--- a/libfdisk/src/fdiskP.h
+++ b/libfdisk/src/fdiskP.h
@@ -264,6 +264,7 @@ struct fdisk_ask {
 
 struct fdisk_context {
 	int dev_fd;         /* device descriptor */
+	int readonly;       /* is the device read-only? */
 	char *dev_path;     /* device path */
 	unsigned char *firstsector; /* buffer with master boot record */
 
-- 
1.9.0


  reply	other threads:[~2014-02-27  3:24 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Maciej Małecki [this message]
2014-02-28  2:32 ` 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

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=1393471600-17777-2-git-send-email-me@mmalecki.com \
    --to=me@mmalecki.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.