Chinaunix首页 | 论坛 | 博客
  • 博客访问: 743589
  • 博文数量: 285
  • 博客积分: 11
  • 博客等级: 民兵
  • 技术积分: 2340
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-23 17:58
个人简介

真正的安全感,来自你对自己的信心,是你每个阶段性目标的实现,而真正的归属感,在于你的内心深处,对自己命运的把控,因为你最大的对手永远都是自己。| Network+Security+Database.

文章存档

2011年(3)

2010年(1)

2009年(33)

2008年(248)

我的朋友

分类: LINUX

2008-08-27 19:23:18

/*********create ,read and write a /proc file based on linux******/
#include
#include
#include
#include
#include

#define PROCFS_NAME "myprocfile"
#define PROCFS_MAX_SIZE 1024

char proc_buffer[PROCFS_MAX_SIZE];
static long int proc_buffer_size=0;

struct proc_dir_entry *MY_PROC_FILE;

int
 procfile_read(char*buffer, char**buffer_localation, off_t offset,\
                            int buffer_length,int* eof, void *data )
                {
          int ret;
          printk(KERN_INFO "procfile_read(/proc/%s) called\n",PROCFS_NAME);
          if(offset>0)
                         {
              printk(KERN_INFO "file /porc/%s has been finished to read\n",PROCFS_NAME);
              return 0;
          }else
                  {
                        memcpy(buffer,proc_buffer,proc_buffer_size);
                        ret=proc_buffer_size;
                          
                      }
             
        return ret;
                  }
                 
int
procfile_write(struct file *filp, const char *buffer,unsigned long count,void *data)
                 {
                 proc_buffer_size=count;
         if(proc_buffer_size > PROCFS_MAX_SIZE)
                                      {
                      proc_buffer_size = PROCFS_MAX_SIZE;
                                       }
          if(copy_from_user(proc_buffer, buffer, proc_buffer_size));
                                       {
                     return -EFAULT;
                                       }
                                      
       return proc_buffer_size;
    
                  }
                 
                 
   
 static int __init procfs_init(void)
 {
 MY_PROC_FILE = create_proc_entry(PROCFS_NAME,0644,NULL);
  if(MY_PROC_FILE == NULL)
  {
  remove_proc_entry(PROCFS_NAME, &proc_root);
  printk(KERN_INFO "Error:could not initialize /proc/%s\n",PROCFS_NAME);
  return -ENOMEM;
  }
  MY_PROC_FILE->read_proc = procfile_read;
  MY_PROC_FILE->write_proc = procfile_write;
  MY_PROC_FILE->owner = THIS_MODULE;
  MY_PROC_FILE->mode = S_IFREG | S_IRUGO;
  MY_PROC_FILE->uid = 0;
  MY_PROC_FILE->gid =0;
  MY_PROC_FILE->size=37;
   
    printk(KERN_INFO "/proc/%s is created\n",PROCFS_NAME);
    return 0;
 }
 
 
 static void __exit procfs_exit(void)
 {
   remove_proc_entry(PROCFS_NAME, &proc_root);
   printk(KERN_INFO "/proc/%s has been removed\n",PROCFS_NAME);
 }
 
 
 module_init(procfs_init);
 module_exit(procfs_exit);

/***********************************************************************/
######### Makefile ####################
obj-m += procfs2.o
all:
    make -C /lib/modules/$(shell uname -r)/build     M=$(PWD) modules
    insmod procfs2.ko
clean:
    rmmod procfs2
    #make -C /lib/modules/$(shell uname -r)/build  M=$(PWD) clean
 
解释:
  clean 中的make这行命令不要和rmmod同使用。实践证明在make all后,用make -C        /lib/modules/$(shell uname -r)/build  M=$(PWD) clean 命令或者用rmmod命令都可以
来注销上面加入的模块procfs2。可以利用命令demsg查看内核打印出来的最后两行信息如下:
 /proc/myprocfile is created
 /proc/myprocfile has been removed
说明可以成功加载和卸载模块procfs2。
注意其实跟一般的linux设备驱动一样,我们也把/proc下面的文件当成内核模块,用insmod插入到内核中运行,insmod成功后你可以用命令cat /proc/modules | grep procfs2找到procfs2作为模块已经被插入到内核中。而在同时你可以在/proc文件系统下可以看到该模块对于的文件myprocfile. 当然可以用命令
rmmod procfs2卸载掉该模块。
/proc文件系统和内核的通信的具体内容可以参看下面列出的网址上的文章。

以上程序由本人编写,编写过程中参考了以下两篇文章:http://www.ibm.com/developerworks/cn/linux/l-proc.html

向以上两位作者表示感谢!


 
阅读(1683) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

2011-06-26 17:28:27

学习了,多谢楼主分享哦!也欢迎广大linux爱好者来我们的论坛一起讨论arm哦!www.lt-net.cn

chinaunix网友2008-12-21 21:26:43

if(copy_from_user(proc_buffer, buffer, proc_buffer_size)); 这一句多个了;号