【内核】:一个使用 seq_file 接口的 proc_fs 例子
2011-10-12 23:40
465 查看
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <asm/uaccess.h>
static struct proc_dir_entry *pfile;
static char *myfruits[5] = {"apple", "orange", "banana", "watermelon", "pear"};
static void *my_seq_start(struct seq_file *s, loff_t *pos)
{
if (*pos >= 5)
return NULL;
return myfruits[*pos];
}
static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
if (++*pos >= 5)
return NULL;
return myfruits[*pos];
}
static void my_seq_stop(struct seq_file *s, void *v)
{
// nothing to do;
}
static int my_seq_show(struct seq_file *s, void *v)
{
seq_printf(s, "%s ", (char *)v);
return 0;
}
static struct seq_operations proc_seq_ops = {
.start = my_seq_start,
.next = my_seq_next,
.stop = my_seq_stop,
.show = my_seq_show,
};
static int my_seq_open(struct inode *inode, struct file *file)
{
return seq_open(file, &proc_seq_ops);
}
static struct file_operations proc_fops = {
.open = my_seq_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
static int __init myproc_init(void)
{
pfile = proc_create("fruits", 0666, NULL, &proc_fops);
if (!pfile) {
printk(KERN_ERR "Can't create /proc/fruits/n");
remove_proc_entry("mydir", NULL);
return -1;
}
return 0;
}
static void __exit myproc_exit(void)
{
remove_proc_entry("fruits", NULL);
}
module_init(myproc_init);
module_exit(myproc_exit);
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <asm/uaccess.h>
static struct proc_dir_entry *pfile;
static char *myfruits[5] = {"apple", "orange", "banana", "watermelon", "pear"};
static void *my_seq_start(struct seq_file *s, loff_t *pos)
{
if (*pos >= 5)
return NULL;
return myfruits[*pos];
}
static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
if (++*pos >= 5)
return NULL;
return myfruits[*pos];
}
static void my_seq_stop(struct seq_file *s, void *v)
{
// nothing to do;
}
static int my_seq_show(struct seq_file *s, void *v)
{
seq_printf(s, "%s ", (char *)v);
return 0;
}
static struct seq_operations proc_seq_ops = {
.start = my_seq_start,
.next = my_seq_next,
.stop = my_seq_stop,
.show = my_seq_show,
};
static int my_seq_open(struct inode *inode, struct file *file)
{
return seq_open(file, &proc_seq_ops);
}
static struct file_operations proc_fops = {
.open = my_seq_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
static int __init myproc_init(void)
{
pfile = proc_create("fruits", 0666, NULL, &proc_fops);
if (!pfile) {
printk(KERN_ERR "Can't create /proc/fruits/n");
remove_proc_entry("mydir", NULL);
return -1;
}
return 0;
}
static void __exit myproc_exit(void)
{
remove_proc_entry("fruits", NULL);
}
module_init(myproc_init);
module_exit(myproc_exit);
相关文章推荐
- 一个使用 seq_file 接口的 proc_fs 例子
- 一个使用 seq_file 接口的 proc_fs 例子
- Linux内核学习笔记之seq_file接口创建可读写proc文件
- CodeProject - 在C#使用SHGetFileInfo获取(管理)文件或者文件夹图标(C#封装Win32函数的一个例子)
- Flume例子:使用netcat和exec作为信源,logger和file作为信宿,并分别为每对信源和信宿实现一个信道
- 内核proc文件系统与seq接口(2)---内核proc文件系统编程接口
- 内核proc文件系统与seq接口(3)---内核proc文件底层结构浅析
- 转载只为记录经典之<用户空间内核空间ipc总结(sockopt,ioctl,mmap,netlink,proc,seq,file,copy_user)>
- 内核proc文件系统与seq接口(4)---seq_file接口编程浅析
- 使用seq_file,实现大文件的/proc file
- 用户空间内核空间ipc总结(sockopt,ioctl,mmap,netlink,proc,seq,file,copy_user)
- 用户空间内核空间ipc总结(sockopt,ioctl,mmap,netlink,proc,seq,file,copy_user)
- 使用proc接口例子【转】
- 用户空间内核空间ipc总结(sockopt,ioctl,mmap,netlink,proc,seq,file,copy_user)
- 内核proc文件系统与seq接口----内核proc文件系统编程接口
- 【内核】:proc_fs 使用说明
- seq_file接口创建可读写proc文件
- 使用jmeter进行接口压力测试的一个简单例子
- 内核proc文件系统与seq接口(1)---内核proc文件系统简介
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对