查看: 9255|回复: 0

linux kernel proc文件系统

[复制链接]
发表于 2010-11-9 17:00:29 | 显示全部楼层 |阅读模式
台州网址导航
在linux内核模块开发中如果需要用到文件IO,使用proc文件系统是一个很好的方法。

  给大家转一个例子供参考

  /**

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

  *

  */

  #include <linux/module.h> /* Specifically, a module */

  #include <linux/kernel.h> /* We're doing kernel work */

  #include <linux/proc_fs.h> /* Necessary because we use the proc fs */

  #include <asm/uaccess.h> /* 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);

  }
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州维博网络(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:企业网站建设、网站程序开发、手机APP客户端、平面设计、主机域名、虚拟空间、网站推广、网站优化、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2024 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表