一个使用 seq_file 接口的 proc_fs 例子

#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/module.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/lockdep.h> static int lockdep_enabled = 1; static int lockdep_proc_show(struct seq_file *m, void *v) { seq_printf(m, "%d\n", lockdep_enabled); return 0; } static ssize_t lockdep_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { char buf[32]; if (count > sizeof(buf) - 1) return -EINVAL; if (copy_from_user(buf, buffer, count)) return -EFAULT; buf[count] = '\0'; if (strcmp(buf, "0\n") == 0) { lockdep_enabled = 0; pr_info("lockdep disabled\n"); } else if (strcmp(buf, "1\n") == 0) { lockdep_enabled = 1; pr_info("lockdep enabled\n"); } else { return -EINVAL; } return count; } static int lockdep_proc_open(struct inode *inode, struct file *file) { return single_open(file, lockdep_proc_show, NULL); } static const struct file_operations lockdep_proc_fops = { .owner = THIS_MODULE, .open = lockdep_proc_open, .read = seq_read, .write = lockdep_proc_write, .llseek = seq_lseek, .release = single_release, }; static int __init lockdep_init(void) { struct proc_dir_entry *entry = proc_create("lockdep", 0666, NULL, &lockdep_proc_fops); if (!entry) { pr_err("failed to create /proc/lockdep\n"); return -ENOMEM; } pr_info("lockdep module loaded\n"); return 0; } static void __exit lockdep_exit(void) { remove_proc_entry("lockdep", NULL); pr_info("lockdep module unloaded\n"); } module_init(lockdep_init); module_exit(lockdep_exit); MODULE_LICENSE("GPL");这个程序哪里实现了对lockdep工具的控制
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值