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@xxxxxxxxxxxx> --- 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 c405403..dc9f0f9 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; @@ -262,6 +264,7 @@ int fdisk_context_assign_device(struct fdisk_context *cxt, } 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 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html