自动创建proc文件

#include <linux/module.h>  
#include <linux/sched.h>  
#include <linux/proc_fs.h>  
#include <linux/seq_file.h>  
#include <linux/uaccess.h>  
#include <linux/slab.h>  


typedef struct proc_private_data_s {
    void *data;
    unsigned char type;
} proc_private_data_t;

enum {
    INT_TYPE,
    STRING_TYPE,
    MAX_TYPE
};

void show_int_proc_private_data(struct seq_file *m, proc_private_data_t * private_data)
{
    seq_printf(m, "%d\n", *((int*)private_data->data));
}

void show_string_proc_private_data(struct seq_file *m, proc_private_data_t * private_data)
{
    seq_printf(m, "%s\n", (char *)private_data->data);
}


typedef struct show_private_data_s {
    unsigned char type;
    void (*func)(struct seq_file *m, proc_private_data_t *private_data);
} show_private_data_t;


show_private_data_t show_arrays[] = {
    {INT_TYPE, show_int_proc_private_data},
    {STRING_TYPE, show_string_proc_private_data}
};



static void *proc_seq_start(struct seq_file *m, loff_t *pos)  
{  
    if (0 == *pos)  
    {  
        ++*pos;  
        return (void *)1; // return anything but NULL, just for test  
    }  
    return NULL;  
} 

static void *proc_seq_next(struct seq_file *m, void *v, loff_t *pos)  
{  
    return NULL;       
} 


static void proc_seq_stop(struct seq_file *m, void *v)  
{  
}


static int proc_seq_show(struct seq_file *m, void *v)  
{  
    struct proc_dir_entry *pde = m->private;
    proc_private_data_t *private_data = pde->data;
    show_arrays[private_data->type].func(m, private_data);

    return 0; //!! must be 0, or will show nothing T.T  
}

static struct seq_operations proc_seq_fops =   
{  
    .start  = proc_seq_start,  
    .next   = proc_seq_next,  
    .stop   = proc_seq_stop,  
    .show   = proc_seq_show,  
};  

static int proc_seq_open(struct inode *inode, struct file *file)  
{  
    int ret = seq_open(file, &proc_seq_fops);  

    if (!ret) {
        struct seq_file *sf = file->private_data;
        sf->private = PDE(inode);
    }
    return ret;
}  


static struct file_operations proc_file_ops =   
{  
    .owner      = THIS_MODULE,  
    .open       = proc_seq_open,  
    .read       = seq_read,  
    .llseek     = seq_lseek,  
    .release    = seq_release,  
}; 
int register_proc_file(const char *proc_file, proc_private_data_t *data)
{
    struct proc_dir_entry *file;

    file = proc_create_data(proc_file, 0666, NULL, &proc_file_ops, data);
    if (file == NULL) {
        printk("Can't create proc file %s\n", proc_file);
        return -1;
    }
    return 0;
}
EXPORT_SYMBOL(register_proc_file);

proc_private_data_t private_data;

static int __init my_init(void)  
{
    private_data.type = STRING_TYPE;
    private_data.data = kzalloc(strlen("wgw"), GFP_KERNEL);
    memcpy(private_data.data, "wgw", strlen("wgw"));
    return register_proc_file("jif", &private_data);
}  


static void __exit my_exit(void)  
{  
    kfree(private_data.data);
    remove_proc_entry("jif", NULL);  
}  

module_init(my_init);  
module_exit(my_exit);  

MODULE_AUTHOR("ah");  
MODULE_LICENSE("GPL");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值