• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            seq_file源碼分析

            struct seq_operations {
            void * (*start) (struct seq_file *m, loff_t *pos);
            void (*stop) (struct seq_file *m, void *v);
            void * (*next) (struct seq_file *m, void *v, loff_t *pos);
            int (*show) (struct seq_file *m, void *v);
            };
            seq_file的使用
            struct file_operations exam_seq_file_ops = {
            .owner   = THIS_MODULE,
            .open    = exm_seq_open,
            .read    = seq_read,
            .llseek  = seq_lseek,
            .release = seq_release
            };
            static int exam_seq_open(struct inode *inode, struct file *file)
            {
            return seq_open(file, &exam_seq_ops);
            };

            struct proc_dir_entry *entry;
                        entry = create_proc_entry("exam_seq_file", 0, NULL);
                        if (entry)
                        entry->proc_fops = &exam_seq_file_ops;


            當(dāng)用戶態(tài)的程序打開這個(gè)對(duì)應(yīng)的proc目錄時(shí),open->sysopen->exm_seq_open->seq_open(file,&exam_seq_ops),下面我們主要來(lái)看下seq_open函數(shù)
            int seq_open(struct file *file, const struct seq_operations *op)
            {
             struct seq_file *p = file->private_data;

             if (!p) {
              p = kmalloc(sizeof(*p), GFP_KERNEL); 
              if (!p)
               return -ENOMEM;
              file->private_data = p;
             }
             memset(p, 0, sizeof(*p));
             mutex_init(&p->lock);
             p->op = op;

             /*
              * Wrappers around seq_open(e.g. swaps_open) need to be
              * aware of this. If they set f_version themselves, they
              * should call seq_open first and then set f_version.
              */
             file->f_version = 0;

             /* SEQ files support lseek, but not pread/pwrite */
             file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
             return 0;
            }
            struct seq_file {
             char *buf;
             size_t size;
             size_t from;
             size_t count;
             loff_t index;
             u64 version;
             struct mutex lock;
             const struct seq_operations *op;
             void *private;
            };
            再來(lái)看下seq_read函數(shù)
            /**
             * seq_read - ->read() method for sequential files.
             * @file: the file to read from
             * @buf: the buffer to read to
             * @size: the maximum number of bytes to read
             * @ppos: the current position in the file
             *
             * Ready-made ->f_op->read()
             */
            ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
            {
             struct seq_file *m = (struct seq_file *)file->private_data;
             /* grab buffer if we didn't have one */
             if (!m->buf) {
              m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
              if (!m->buf)
               goto Enomem;
             }
             p = m->op->start(m, &pos);
             while (1) {
              err = m->op->show(m, p);

               p = m->op->next(m, p, &pos);

              m->op->stop(m, p);
              }
            } //可見seq_read函數(shù)分別調(diào)用了自定義的seq_operation結(jié)構(gòu)中的回調(diào)函數(shù)start,show,next,stop等函數(shù)。
            這樣就將vfs層的sys_read->seq_read(調(diào)用我們自己定義的seq_operation的start,show,next,stop等函數(shù)),從而可以根據(jù)我們自己選擇的方式去顯示我們的數(shù)據(jù)。

             關(guān)于proc,seq_file的使用和介紹參考:

            在 Linux 下用戶空間與內(nèi)核空間數(shù)據(jù)交換的方式,第 2 部分: procfs、seq_file、debugfs和relayfs

             

            http://www.ibm.com/developerworks/cn/linux/l-kerns-usrs2/index.html

            posted on 2010-12-16 22:40 lstar 閱讀(1548) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            導(dǎo)航

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            文章檔案

            搜索

            最新評(píng)論

            欧美成a人片免费看久久| 久久久久一本毛久久久| 久久精品免费全国观看国产| 九九精品久久久久久噜噜| 99久久这里只有精品| 久久久亚洲裙底偷窥综合| 久久青草国产手机看片福利盒子 | 日韩精品久久久久久久电影| 久久笫一福利免费导航 | 久久精品国产福利国产秒| 久久这里只有精品首页| 久久不见久久见免费视频7| 久久久久久久波多野结衣高潮| 色综合久久无码中文字幕| 思思久久好好热精品国产| 国产亚洲色婷婷久久99精品| 人妻无码久久一区二区三区免费 | 久久久精品国产Sm最大网站| 久久久国产乱子伦精品作者| 国产 亚洲 欧美 另类 久久| 色综合久久综精品| 77777亚洲午夜久久多喷| 久久人人妻人人爽人人爽| 亚洲精品无码久久久| 久久伊人精品一区二区三区| 久久99热国产这有精品| 精品久久人妻av中文字幕| 伊人久久大香线蕉综合影院首页| 94久久国产乱子伦精品免费 | 久久精品这里只有精99品| 91精品国产91久久综合| 午夜精品久久久久久毛片| 7777久久久国产精品消防器材| 亚洲中文字幕伊人久久无码| 亚洲国产成人精品91久久久| 人人妻久久人人澡人人爽人人精品| 99热成人精品免费久久| 伊人热人久久中文字幕| 97精品国产97久久久久久免费| 久久精品国内一区二区三区| 久久成人精品视频|