From: Eric Sandeen Date: Fri, 10 Oct 2008 22:17:43 +0000 (-0500) Subject: unix_io: check for read-only devices when opening R/W X-Git-Tag: v1.41.3~7 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=7ed7a4b6ed8b2fce891874a0eafdc8f77c3ffc34;p=tools%2Fe2fsprogs.git 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 --- 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)))