linux proc fs node

linux proc fs node



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

#ifdef CONFIG_PROC_FS

extern int xx_node_val;

static int xx_node_proc_show(struct seq_file *m, void *v)
{
	seq_printf(m, "%d\n", console_klog_enable);

	return 0;
}

static int xx_node_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, xx_node_proc_show, PDE_DATA(inode));
}

static ssize_t xx_node_proc_write(struct file *file, const char __user *buf,
	size_t count, loff_t *pos)
{
	char lbuf[10] = {0};
	int val;

	/* if (!capable(CAP_SYS_ADMIN))
		return -EACCES; */

	if (count >= sizeof(lbuf))
		count = sizeof(lbuf)-1;

	if (copy_from_user(lbuf, buf, count))
		return -EFAULT;

	lbuf[count] = 0;

	sscanf(lbuf, "%d", &val);

	if (val == 0) {
		xx_node_val = 0;
	} else if (val == 1) {
		xx_node_val = 1;
	} else {
		pr_warn("%s() set val:%d, invalid argument, expect '0' or '1'\n", 
			__func__, val);

		return -EINVAL;	
	}

	return count;
}

static const struct file_operations xx_node_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= xx_node_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= xx_node_proc_write,
};

static int __init xx_node_create_procfs(void)
{
	struct proc_dir_entry *ent;

	ent = proc_create_data("xx_node", S_IRUGO|S_IWUSR, NULL,
					&xx_node_proc_fops, NULL);
	if (ent == NULL)
		return -1;

	return 0;
}

module_init(xx_node_create_procfs)
#endif



posted @ 2025-09-18 12:02  王阳开  阅读(3)  评论(0)    收藏  举报