linux kernel proc文件系统_绿色软件之家

绿色软件之家:您身边最放心的安全下载站! 最新更新|软件分类|软件专题|手机版|论坛转贴|软件发布

您当前所在位置: 首页操作系统LINUX → linux kernel proc文件系统

linux kernel proc文件系统

时间:2015-06-28 00:00:00 来源:绿色软件之家 作者:网管联盟 我要评论(0)

  在linux内核模块开发中如果需要用到文件IO,使用proc文件系统是一个很好的方法。

  给大家转一个例子供参考

  /**

  *? 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) called\n", 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/%s\n",

  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 created\n", 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 removed\n", PROCFS_NAME);

  }

关键词标签:linux

相关阅读

文章评论
发表评论

热门文章 安装红帽子RedHat Linux9.0操作系统教程 安装红帽子RedHat Linux9.0操作系统教程 Tomcat9.0如何安装_Tomcat9.0环境变量配置方法 Tomcat9.0如何安装_Tomcat9.0环境变量配置方法 多种操作系统NTP客户端配置 多种操作系统NTP客户端配置 Linux操作系统修改IP Linux操作系统修改IP

相关下载

    人气排行 Linux下获取CPUID、硬盘序列号与MAC地址 dmidecode命令查看内存型号 linux tc实现ip流量限制 安装红帽子RedHat Linux9.0操作系统教程 linux下解压rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 关机、重启、注销 命令 查看linux服务器硬盘IO读写负载