patch-2.3.13 linux/drivers/char/mem.c

Next file: linux/drivers/char/misc.c
Previous file: linux/drivers/char/lp_m68k.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.3.12/linux/drivers/char/mem.c linux/drivers/char/mem.c
@@ -17,6 +17,7 @@
 #include <linux/joystick.h>
 #include <linux/i2c.h>
 #include <linux/raw.h>
+#include <linux/capability.h>
 
 #include <asm/uaccess.h>
 #include <asm/io.h>
@@ -459,11 +460,23 @@
 	}
 }
 
+static int open_port(struct inode * inode, struct file * filp)
+{
+	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
+}
+
+static int open_mem(struct inode * inode, struct file * filp)
+{
+	return (capable(CAP_SYS_RAWIO)
+		|| !(filp->f_mode & FMODE_WRITE)) ? 0 : -EPERM;
+}
+
 #define mmap_kmem	mmap_mem
 #define zero_lseek	null_lseek
 #define full_lseek      null_lseek
 #define write_zero	write_null
 #define read_full       read_zero
+#define open_kmem	open_mem
 
 static struct file_operations mem_fops = {
 	memory_lseek,
@@ -473,7 +486,7 @@
 	NULL,		/* mem_poll */
 	NULL,		/* mem_ioctl */
 	mmap_mem,
-	NULL,		/* no special open code */
+	open_mem,
 	NULL,		/* flush */
 	NULL,		/* no special release code */
 	NULL		/* fsync */
@@ -487,7 +500,7 @@
 	NULL,		/* kmem_poll */
 	NULL,		/* kmem_ioctl */
 	mmap_kmem,
-	NULL,		/* no special open code */
+	open_kmem,
 	NULL,		/* flush */
 	NULL,		/* no special release code */
 	NULL		/* fsync */
@@ -515,7 +528,7 @@
 	NULL,		/* port_poll */
 	NULL,		/* port_ioctl */
 	NULL,		/* port_mmap */
-	NULL,		/* no special open code */
+	open_port,
 	NULL,		/* flush */
 	NULL,		/* no special release code */
 	NULL		/* fsync */
@@ -598,7 +611,7 @@
 	NULL		/* fsync */
 };
 
-__initfunc(int chr_dev_init(void))
+int __init chr_dev_init(void)
 {
 	if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
 		printk("unable to get major %d for memory devs\n", MEM_MAJOR);

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)