From 7ed7a4b6ed8b2fce891874a0eafdc8f77c3ffc34 Mon Sep 17 00:00:00 2001 From: Eric Sandeen Date: Fri, 10 Oct 2008 17:17:43 -0500 Subject: [PATCH] unix_io: check for read-only devices when opening R/W When we open a device on linux, test whether it is writable right away, rather than trying to proceed and clean up when writes start failing. Signed-off-by: Eric Sandeen Signed-off-by: Theodore Ts'o --- lib/ext2fs/unix_io.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/ext2fs/unix_io.c b/lib/ext2fs/unix_io.c index d77e59d..797fce8 100644 --- a/lib/ext2fs/unix_io.c +++ b/lib/ext2fs/unix_io.c @@ -31,6 +31,12 @@ #ifdef __linux__ #include #endif +#ifdef HAVE_SYS_IOCTL_H +#include +#endif +#ifdef HAVE_SYS_MOUNT_H +#include +#endif #if HAVE_SYS_STAT_H #include #endif @@ -41,6 +47,10 @@ #include #endif +#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE) +#define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */ +#endif + #include "ext2_fs.h" #include "ext2fs.h" @@ -453,6 +463,21 @@ static errcode_t unix_open(const char *name, int flags, io_channel *channel) goto cleanup; } +#ifdef BLKROGET + if (flags & IO_FLAG_RW) { + int error; + int readonly = 0; + + /* Is the block device actually writable? */ + error = ioctl(data->dev, BLKROGET, &readonly); + if (!error && readonly) { + close(data->dev); + retval = EPERM; + goto cleanup; + } + } +#endif + #ifdef __linux__ #undef RLIM_INFINITY #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4))) -- 1.8.3.1