procfs的简单练习
时间:2010-03-31 来源:sandflee
#include <linux/module.h> /*module_init */
#include <linux/proc_fs.h> /*so many */
#include <linux/stat.h> /*S_IRUGO */
#include <asm/uaccess.h> /*copy_from_user*/
#include <linux/jiffies.h> /*jiffies*/
#include <linux/kernel.h>
#define MODULENAME "sandflee"
#define BUFSIZE 1024
MODULE_LICENSE("GPL");
struct proc_dir_entry *proc_dir= NULL;
struct proc_data{
char buf[BUFSIZE];
int count;
struct semaphore sem;
} ;
struct proc_data data;
int jf_read(char *page, char **start, off_t off,
int count, int *eof, void *datas)
{
int len=0;
len=sprintf(page,"jiffies:%lu\n",jiffies);
if(len>count) //if count <0 ,what happened?
len=count;
page[len]=0;
return len;
}
int data_read(char *page, char **start, off_t off,
int count, int *eof, void *datas)
{
int len=0;
down(&data.sem);
len=sprintf(page,"data:%s;count=%d\n",data.buf,data.count);
data.count++;
printk(KERN_ALERT"count ++,%d\n",data.count);
up(&data.sem);
if(len>count)
len=count;
page[len]=0;
return len;
}
int data_write(struct file *file, const char __user *buffer,
unsigned long count, void *datas)
{
int n=0;
if(count > BUFSIZE -1)
count=BUFSIZE-1;
down(&data.sem);
if((n=copy_from_user(&data.buf,buffer,count)!= 0)){
count-=n;
goto out;
}
out:
data.count--;
printk(KERN_ALERT"count --,%d\n",data.count);
up(&data.sem);
return count;
}
static int __init proc_init(void)
{
int rv = 0;
struct proc_dir_entry *jf_entry = NULL;
struct proc_dir_entry *data_entry = NULL;
init_MUTEX(&data.sem);
proc_dir = proc_mkdir(MODULENAME,NULL);
if(proc_dir == NULL){
rv=-ENOMEM;
goto out;
}
proc_dir->owner = THIS_MODULE;
jf_entry=create_proc_entry("jf",S_IRUGO,proc_dir);
if(jf_entry==NULL){
rv=-ENOMEM;
goto out1;
}
jf_entry->uid = 0;
jf_entry->gid = 0;
jf_entry->owner = THIS_MODULE;
jf_entry->read_proc = jf_read;
data_entry= create_proc_entry("data",S_IWUGO|S_IRUGO,proc_dir);
if(data_entry==NULL){
rv=-ENOMEM;
goto out2;
}
data_entry->uid = 0;
data_entry->gid = 0;
data_entry->owner = THIS_MODULE;
data_entry->read_proc = data_read;
data_entry->write_proc = data_write;
return rv;
out2:
remove_proc_entry("jf",proc_dir);
out1:
remove_proc_entry(MODULENAME,NULL);
out:
return rv;
}
static void __exit proc_exit(void)
{
remove_proc_entry("data",proc_dir);
remove_proc_entry("jf",proc_dir);
remove_proc_entry(MODULENAME,NULL);
}
module_init(proc_init);
module_exit(proc_exit);
在/proc目录下建了sandflee目录,里面有两个文件,jf,和data,可以通过jf查看系统的jiffies值,可以往data中写入数据并读出写入的数据,其中读出数据时count++,写入数据时count--,实际测的时候,读出数据的时候count一般加3,不知道为什么。
#include <linux/proc_fs.h> /*so many */
#include <linux/stat.h> /*S_IRUGO */
#include <asm/uaccess.h> /*copy_from_user*/
#include <linux/jiffies.h> /*jiffies*/
#include <linux/kernel.h>
#define MODULENAME "sandflee"
#define BUFSIZE 1024
MODULE_LICENSE("GPL");
struct proc_dir_entry *proc_dir= NULL;
struct proc_data{
char buf[BUFSIZE];
int count;
struct semaphore sem;
} ;
struct proc_data data;
int jf_read(char *page, char **start, off_t off,
int count, int *eof, void *datas)
{
int len=0;
len=sprintf(page,"jiffies:%lu\n",jiffies);
if(len>count) //if count <0 ,what happened?
len=count;
page[len]=0;
return len;
}
int data_read(char *page, char **start, off_t off,
int count, int *eof, void *datas)
{
int len=0;
down(&data.sem);
len=sprintf(page,"data:%s;count=%d\n",data.buf,data.count);
data.count++;
printk(KERN_ALERT"count ++,%d\n",data.count);
up(&data.sem);
if(len>count)
len=count;
page[len]=0;
return len;
}
int data_write(struct file *file, const char __user *buffer,
unsigned long count, void *datas)
{
int n=0;
if(count > BUFSIZE -1)
count=BUFSIZE-1;
down(&data.sem);
if((n=copy_from_user(&data.buf,buffer,count)!= 0)){
count-=n;
goto out;
}
out:
data.count--;
printk(KERN_ALERT"count --,%d\n",data.count);
up(&data.sem);
return count;
}
static int __init proc_init(void)
{
int rv = 0;
struct proc_dir_entry *jf_entry = NULL;
struct proc_dir_entry *data_entry = NULL;
init_MUTEX(&data.sem);
proc_dir = proc_mkdir(MODULENAME,NULL);
if(proc_dir == NULL){
rv=-ENOMEM;
goto out;
}
proc_dir->owner = THIS_MODULE;
jf_entry=create_proc_entry("jf",S_IRUGO,proc_dir);
if(jf_entry==NULL){
rv=-ENOMEM;
goto out1;
}
jf_entry->uid = 0;
jf_entry->gid = 0;
jf_entry->owner = THIS_MODULE;
jf_entry->read_proc = jf_read;
data_entry= create_proc_entry("data",S_IWUGO|S_IRUGO,proc_dir);
if(data_entry==NULL){
rv=-ENOMEM;
goto out2;
}
data_entry->uid = 0;
data_entry->gid = 0;
data_entry->owner = THIS_MODULE;
data_entry->read_proc = data_read;
data_entry->write_proc = data_write;
return rv;
out2:
remove_proc_entry("jf",proc_dir);
out1:
remove_proc_entry(MODULENAME,NULL);
out:
return rv;
}
static void __exit proc_exit(void)
{
remove_proc_entry("data",proc_dir);
remove_proc_entry("jf",proc_dir);
remove_proc_entry(MODULENAME,NULL);
}
module_init(proc_init);
module_exit(proc_exit);
在/proc目录下建了sandflee目录,里面有两个文件,jf,和data,可以通过jf查看系统的jiffies值,可以往data中写入数据并读出写入的数据,其中读出数据时count++,写入数据时count--,实际测的时候,读出数据的时候count一般加3,不知道为什么。
相关阅读 更多 +