IT貓撲網(wǎng):您身邊最放心的安全下載站! 最新更新|軟件分類|軟件專題|手機(jī)版|論壇轉(zhuǎn)貼|軟件發(fā)布

您當(dāng)前所在位置:首頁操作系統(tǒng)LINUX → linux kernel proc文件系統(tǒng)

linux kernel proc文件系統(tǒng)

時(shí)間:2015/6/28來源:IT貓撲網(wǎng)作者:網(wǎng)管聯(lián)盟我要評論(0)

  在linux內(nèi)核模塊開發(fā)中如果需要用到文件IO,使用proc文件系統(tǒng)是一個(gè)很好的方法。

  給大家轉(zhuǎn)一個(gè)例子供參考

  /**

  *  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);

  }

關(guān)鍵詞標(biāo)簽:linux

相關(guān)閱讀

文章評論
發(fā)表評論

熱門文章 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程使用screen管理你的遠(yuǎn)程會話使用screen管理你的遠(yuǎn)程會話GNU/Linux安裝vmwareGNU/Linux安裝vmware如何登錄linux vps圖形界面 Linux遠(yuǎn)程桌面連如何登錄linux vps圖形界面 Linux遠(yuǎn)程桌面連

相關(guān)下載

人氣排行 Linux下獲取CPUID、硬盤序列號與MAC地址linux tc實(shí)現(xiàn)ip流量限制dmidecode命令查看內(nèi)存型號linux下解壓rar文件安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程Ubuntu linux 關(guān)機(jī)、重啟、注銷 命令lcx.exe、nc.exe、sc.exe入侵中的使用方法查看linux服務(wù)器硬盤IO讀寫負(fù)載