Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1258478
  • 博文数量: 494
  • 博客积分: 161
  • 博客等级: 入伍新兵
  • 技术积分: 5084
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-01 07:37
个人简介

只有偏执狂才能生存

文章分类

全部博文(494)

文章存档

2016年(10)

2015年(112)

2014年(69)

2013年(275)

2012年(28)

分类:

2012-11-22 13:34:10

原文地址:创建proc文件节点读写 作者:fly123456789

/**

  *  procfs2.c -  create a "file" in /proc

  *

  */

#include /* Specifically, a module */
#include /* We're doing kernel work */
#include /* Necessary because we use the proc fs */
#include /* for copy_from_user */
#define PROCFS_MAX_SIZE  1024
#define PROCFS_NAME   "buffer1k"

  /**

  * This structure hold information about the /proc file

  *

  */

  static struct proc_dir_entry *Our_Proc_File;

  /**

  * The buffer used to store character for this module

  *

  */

  static char procfs_buffer[PROCFS_MAX_SIZE];

  /**

  * The size of the buffer

  *

  */

  static unsigned long procfs_buffer_size = 0;

  /**

  * This function is called then the /proc file is read

  *

  */

  int

  procfile_read(char *buffer,

  char **buffer_location,

  off_t offset, int buffer_length, int *eof, void *data)

  {

  int ret;

  printk(KERN_INFO "procfile_read (/proc/%s) calledn", PROCFS_NAME);

  if (offset > 0) {

  /* we have finished to read, return 0 */

  ret  = 0;

  } else {

  /* fill the buffer, return the buffer size */

  memcpy(buffer, procfs_buffer, procfs_buffer_size);

  ret = procfs_buffer_size;

  }

  return ret;

  }

  /**

  * This function is called with the /proc file is written

  *

  */

  int procfile_write(struct file *file, const char *buffer, unsigned long count,

  void *data)

  {

  /* get buffer size */

  procfs_buffer_size = count;

  if (procfs_buffer_size > PROCFS_MAX_SIZE ) {

  procfs_buffer_size = PROCFS_MAX_SIZE;

  }

  /* write data to the buffer */

  if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {

  return -EFAULT;

  }

  return procfs_buffer_size;

  }

  /**

  *This function is called when the module is loaded

  *

  */

  int init_module()

  {

  /* create the /proc file */

  Our_Proc_File = create_proc_entry(PROCFS_NAME, 0644, NULL);

  if (Our_Proc_File == NULL) {

  remove_proc_entry(PROCFS_NAME, &proc_root);

  printk(KERN_ALERT "Error: Could not initialize /proc/%sn",

  PROCFS_NAME);

  return -ENOMEM;

  }

  Our_Proc_File->read_proc  = procfile_read;

  Our_Proc_File->write_proc = procfile_write;

  Our_Proc_File->owner    = THIS_MODULE;

  Our_Proc_File->mode    = S_IFREG | S_IRUGO;

  Our_Proc_File->uid    = 0;

  Our_Proc_File->gid    = 0;

  Our_Proc_File->size    = 37;

  printk(KERN_INFO "/proc/%s createdn", PROCFS_NAME);

  return 0; /* everything is ok */

  }

  /**

  *This function is called when the module is unloaded

  *

  */

void cleanup_module()
{
    remove_proc_entry(PROCFS_NAME, &proc_root);

  printk(KERN_INFO "/proc/%s removedn", PROCFS_NAME);

  }
阅读(1628) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册