QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5601|回复: 0

关于/proc文件系统及seq_file实现,求解惑!!

[复制链接]
发表于 2013-1-19 10:44:59 | 显示全部楼层 |阅读模式
第一:/proc文件系统是什么 ?这个网上很多,说的很细,我也就不说了。但是他是做什么用的,网上也很多,但是我不解,就我目前了解的,简单的说,/proc是驱动程序的一种调式方法(如有不对可以指出),那么怎样让我的/proc文件系统与我的驱动程序建立联系!!??如下面最简单的一个/proc文件系统例子:
  1. // include kernel head files
  2. #include <linux/init.h>
  3. #include <linux/module.h>

  4. #include <linux/proc_fs.h>
  5. #include <asm/uaccess.h>

  6. // licensed under BSD/GPL
  7. MODULE_LICENSE("Dual BSD/GPL");

  8. static struct proc_dir_entry *prwd_dir = NULL;
  9. static struct proc_dir_entry *prwd_rdonly = NULL;
  10. static struct proc_dir_entry *prwd_rdwr = NULL;

  11. static char sbufr[ 100 ] = "One test string from reading only !\n";
  12. static char sbufrw[ 1000 ] = "";

  13. // read-only function
  14. static int prwd_rdonly_read(char *page, char **start, off_t off, int count, int *eof, void *data)
  15. {
  16.         sprintf( page, "%s", sbufr );

  17.         *eof = 1;

  18.         return strlen( sbufr );
  19. }

  20. // read function for read-write
  21. static int prwd_rdwr_read(char *page, char **start, off_t off, int count, int *eof, void *data)
  22. {
  23.         int len;

  24.         len = strlen( sbufrw );
  25.         if( len <= off ) {
  26.                 printk(KERN_ALERT "File pointer at end of file or no data for reading in read-write buffer !\n");
  27.                 return -EINVAL;
  28.         }

  29.         if( ( len - off ) < count ) {
  30.                 count = len - off;
  31.         }

  32.         memcpy( page, ( sbufrw + off ), count );

  33.         if( len <= ( off + count ) ) {
  34.                 *eof = 1;
  35.         }

  36.         return count;
  37. }

  38. // write function for read-write
  39. static int prwd_rdwr_write(struct file *file, const char __user *buffer, unsigned long count, void *data)
  40. {
  41.         unsigned long cnt = count;

  42.         if( cnt >= sizeof( sbufrw ) ) {
  43.                 cnt = sizeof( sbufrw ) - 1;
  44.         }

  45.         if( copy_from_user( sbufrw, buffer, cnt ) ) {
  46.                 printk(KERN_ALERT "Failure to copy data from user space !\n");
  47.                 return -EFAULT;
  48.         }

  49.         sbufrw[ cnt ] = '\0';

  50.         return cnt;
  51. }

  52. // initialize module function
  53. static int __init procreadwritedemo_init(void)
  54. {
  55.         printk(KERN_ALERT "Entry procreadwritedemo_init !\n");

  56.         // create directory: procreadwritedemo in /proc
  57.         prwd_dir = proc_mkdir( "procreadwritedemo", NULL );
  58.         if( NULL == prwd_dir ) {
  59.                 printk(KERN_ALERT "Cann't create directory: /proc/procreadwritedemo !\n");
  60.                 goto failure_create_dir;
  61.         }
  62.         printk(KERN_ALERT "Success to create directory: /proc/procreadwritedemo !\n");

  63.         // create read-only file: rdonly in /proc/procreadwritedemo
  64.         prwd_rdonly = create_proc_read_entry( "rdonly", 0400, prwd_dir, prwd_rdonly_read, NULL );
  65.         if( NULL == prwd_rdonly ) {
  66.                 printk(KERN_ALERT "Cann't create read-only file: /proc/procreadwritedemo/rdonly !\n");
  67.                 goto failure_create_rdonly;
  68.         }
  69.         printk(KERN_ALERT "Success to create read-only file: /proc/procreadwritedemo/rdonly !\n");

  70.         // create read-write file: rdwr in /proc/procreadwritedemo
  71.         prwd_rdwr = create_proc_entry( "rdwr", 0666, prwd_dir );
  72.         if( NULL == prwd_rdwr ) {
  73.                 printk(KERN_ALERT "Cann't create read-write file: /proc/procreadwritedemo/rdwr !\n");
  74.                 goto failure_create_rdwr;
  75.         }
  76.         prwd_rdwr->read_proc = prwd_rdwr_read;
  77.         prwd_rdwr->write_proc = prwd_rdwr_write;
  78.         printk(KERN_ALERT "Success to create read-write file: /proc/procreadwritedemo/rdwr !\n");

  79.         return 0;

  80. failure_create_rdwr:
  81.         remove_proc_entry( "rdonly", prwd_dir );
  82. failure_create_rdonly:
  83.         remove_proc_entry( "procreadwritedemo", NULL );
  84. failure_create_dir:
  85.         return -EFAULT;
  86. }

  87. // clean up module function
  88. static void __exit procreadwritedemo_exit(void)
  89. {
  90.         printk(KERN_ALERT "Entry procreadwritedemo_exit !\n");

  91.         // remove files and directory created in /proc
  92.         remove_proc_entry( "rdwr", prwd_dir );
  93.         remove_proc_entry( "rdonly", prwd_dir );
  94.         remove_proc_entry( "procreadwritedemo", NULL );
  95. }

  96. // the declarations of initilization and cleaning up module funcitons
  97. module_init( procreadwritedemo_init );
  98. module_exit( procreadwritedemo_exit );

  99. // module information
  100. MODULE_AUTHOR("wensheng");
  101. MODULE_DESCRIPTION("Proc filesystem read/write demo");
  102. MODULE_VERSION("0.0.1");
  103. MODULE_ALIAS("ProcReadWriteDemo");
复制代码
上面的一例,完全是一个独立/proc系统代码的实现,问题是:他是那怎与我想调试的驱动(如一个块设备)程序建立关系了,如是不能跟我的相关驱动建联系,那/proc文件系统存在意义是什么 (简单说就是做什么用的,最好可以举个易懂的例子)??


第二:关于seq_file机制
有如下代码
  1. #include <linux/fs.h>
  2. #include <linux/init.h>
  3. #include <linux/proc_fs.h>
  4. #include <linux/seq_file.h>
  5. struct proc_dir_entry proc_root;

  6. void * our_seq_start(struct seq_file *m, loff_t *pos)
  7. {
  8. //printk("<0>""in our_seq_start/n");
  9. if(*pos == 0)
  10.   return pos;
  11. return NULL;
  12. }

  13. void * our_seq_stop(struct seq_file *m, void *v)
  14. {
  15. //printk("/nDO NOTHING/n");
  16. }


  17. int our_seq_show(struct seq_file *m, loff_t *pos)
  18. {
  19. static unsigned long counter = 0;
  20. int i;
  21. for(i=0;i<10;i++)
  22. seq_printf(m,"the counter is %d/n",i);
  23. return 0;
  24. }

  25. void *our_seq_next(struct seq_file *m, loff_t *pos)
  26. {
  27. static unsigned long counter_next = 0;
  28. if(pos < 1)
  29. {
  30.   counter_next++;
  31.   pos++;
  32.   return pos;
  33. }
  34. else
  35. {
  36.   *pos = 0;
  37.   return NULL;
  38. }
  39. }

  40. static const struct seq_operations our_seq_ops = {
  41. .start = our_seq_start,
  42. .next  = our_seq_next,
  43. .stop  = our_seq_stop,
  44. .show  = our_seq_show
  45. };

  46. static int our_seq_open(struct inode *inode, struct file *filp)
  47. {
  48. printk("<0>""in our_seq_open/n");
  49. return seq_open(filp, &our_seq_ops);
  50. }
  51. int our_ioctl()
  52. {
  53. printk("<0>""in our_seq_ioctl/n");
  54. return 0;
  55. }

  56. int our_write()
  57. {
  58. printk("<0>""in our_seq_write/n");
  59. return 5;
  60. }
  61. struct file_operations our_proc_ops = {
  62. .open    = our_seq_open,
  63. .read    = seq_read,
  64. .write   = our_write,
  65. .llseek  = seq_lseek,
  66. .ioctl   = our_ioctl,
  67. .release = seq_release
  68. };

  69. int our_init()
  70. {
  71. proc_create("hehe", 0, NULL, &our_proc_ops);
  72. return 0;
  73. }
  74. void our_exit()
  75. {
  76. remove_proc_entry("hehe",NULL);
  77. }
  78. module_init(our_init);
  79. module_exit(our_exit);
复制代码
上面代码是目前我在网上查到的/proc文件系统实现中唯一个涉及到ioctl操作(此处不关心代码是否有错,关心功能码是否以实现),因为之前所有相关seq_file机制建立/proc,中struct file_operations,只有如下操作
  1. struct file_operations our_proc_ops = {
  2. .open    = our_seq_open,
  3. .read    = seq_read,
  4. .llseek  = seq_lseek,
  5. .release = seq_release
  6. };
复制代码
而且我们只用实现open(),而read,lseek,release,都是seq_file内部实现,我们可不管。问题是没有write(),没有ioctl()(为什么没有,我们可以暂不讨论),我想问的是,我们可否像上面那个代码一样添加自己的write与ioctl功能。
      以上都是自己跟据现有资做出的理解,如有不对,请您不吝指出,还有我的一些不解之处,求大神解惑!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-12-22 01:49 , Processed in 0.109926 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

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