This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int init_module_rwbuf() | |
{ | |
register_chrdev(277,DEVICE_NAME,&rwbuf_fops); | |
printk(KERN_ALERT "register ok\n"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int rwbuf_open(struct inode *node, struct file *filep) | |
{ | |
if(inuse==1) | |
return -1; | |
inuse=1; | |
printk(KERN_ALERT "open ok\n"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static ssize_t rwbuf_write(struct file *filep, const char *buf, size_t count, loff_t *ppos) | |
{ | |
if(count<=1024) | |
{ | |
writenum++; | |
//printk(KERN_ALERT "write1\n"); | |
copy_from_user(rwbuf,buf,count); | |
//printk(KERN_ALERT "write2\n"); | |
rwlen = count; | |
//printk(KERN_ALERT "write ok\n"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static ssize_t rwbuf_read(struct file* filp, char *buf , size_t count, loff_t *ppos) | |
{ | |
printk(KERN_ALERT "read\n"); | |
count=rwlen; | |
copy_to_user(buf,rwbuf,count); | |
printk(KERN_ALERT "read ok\n"); | |
return count; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<sys/unistd.h> | |
#include<fcntl.h> | |
int main() | |
{ | |
char yourmsg[1000]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<linux/kernel.h> | |
#include<linux/module.h> | |
#include<linux/fs.h> | |
#include<linux/errno.h> | |
#include<linux/uaccess.h> | |
#include<linux/init.h> | |
#define RWBUF_SIZE 2048 | |
#define DEVICE_NAME "ChrDev" //修改为自己喜欢的名字 | |
#define RWBUF_CLEAR 0x909090 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static struct file_operations rwbuf_fops = | |
{ | |
read: rwbuf_read, | |
write: rwbuf_write, | |
ioctl: rwbuf_ioctl, | |
open: rwbuf_open, | |
release: rwbuf_close, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj-m := rwbuf.o | |
PWD := $(shell pwd) | |
KDIR=/usr/src/linux-headers-2.6.35-22-generic | |
default: | |
make -C $(KDIR) M=$(PWD) modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int rwbuf_ioctl(struct inode *node, struct file * filep, unsigned int cmd, unsigned long arg) | |
{ | |
if(cmd==RWBUF_CLEAR) | |
{ | |
rwlen = 0; | |
printk(KERN_ALERT "rwbuf in kernel zero-ed\n"); | |
return 0; | |
} | |
else if(cmd==RWBUF_LENGTH) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
int main() | |
{ | |
system("echo 'this is a test' > /proc/13081070/keke"); | |
} |