Skip to content

Instantly share code, notes, and snippets.

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/proc_fs.h>
#include<linux/sched.h>
#include<linux/uaccess.h>
#define STRINGLEN 1024
char global_buffer[STRINGLEN];
struct proc_dir_entry *example_dir,*zhang_file,*keke_file;
#include<stdio.h>
#include<stdlib.h>
#include<sys/unistd.h>
#include<fcntl.h>
int main()
{
char yourmsg[1000];
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)
{
@kerie
kerie / read.c
Created November 19, 2010 17:08
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;
}
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");
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;
}
int init_module_rwbuf()
{
register_chrdev(277,DEVICE_NAME,&rwbuf_fops);
printk(KERN_ALERT "register ok\n");
return 0;
}
obj-m := rwbuf.o
PWD := $(shell pwd)
KDIR=/usr/src/linux-headers-2.6.35-22-generic
default:
make -C $(KDIR) M=$(PWD) modules
static struct file_operations rwbuf_fops =
{
read: rwbuf_read,
write: rwbuf_write,
ioctl: rwbuf_ioctl,
open: rwbuf_open,
release: rwbuf_close,
};
#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