linux开发接口定义模板,Linux内核开发:创建proc文件并与用户空间接口

目录

Proc文件系统

创建一个新的Proc文件

实现读取处理程序

与用户空间交换数据

实现写处理程序

用户空间应用

在第一篇文章中,我们构建了一个具有初始化和退出功能的简单内核模块 ,并介绍了内核编程中的基本概念。

接下来,我们添加了一个内核模块参数来配置内核模块数据

在本文中,我们将使用procfs(/ proc)文件创建用户空间应用程序的第一个界面

Proc文件系统

Proc是用于与内核内部数据结构接口的伪文件系统。作为用户,您可以使用proc文件进行系统诊断-CPU,内存,中断等。您还可以配置许多参数,例如调度程序参数,内核对象,内存等

与proc的常见交互是使用cat和shell中的echo。

例如:

# cat /proc/cpuinfo

# echo "50"> /proc/sys/kernel/sched_rr_timeslice_ms

创建一个新的Proc文件

要创建proc文件系统,我们需要实现一个简单的接口–file_operation。

我们可以实现20多个功能,但常见的操作是读取,写入。要注册接口,请使用proc_create函数

基本结构是:

#include

#include

#include

#include

#include

#include

#define BUFSIZE 100

MODULE_LICENSE("Dual BSD/GPL");

MODULE_AUTHOR("Liran B.H");

static struct proc_dir_entry *ent;

static ssize_t mywrite(struct file *file, const char __user *ubuf,size_t count, loff_t *ppos)

{

printk( KERN_DEBUG "write handler\n");

return -1;

}

static ssize_t myread(struct file *file, char __user *ubuf,size_t count, loff_t *ppos)

{

printk( KERN_DEBUG "read handler\n");

return 0;

}

static struct file_operations myops =

{

.owner = THIS_MODULE,

.read = myread,

.write = mywrite,

};

static int simple_init(void)

{

ent=proc_create("mydev",0660,NULL,&myops);

return 0;

}

static void simple_cleanup(void)

{

proc_remove(ent);

}

module_init(simple_init);

module_exit(simple_cleanup);

如果构建并插入模块,将看到一个新文件/ proc / mydev,可以使用cat和echo测试读写操作(仅查看内核日志消息)

# echo "test" > /proc/mydev

bash: echo: write error: Operation not permitted

# cat /proc/mydev

# dmesg | tail -2

[ 694.640306] write handler

[ 714.661465] read handler

实现读取处理程序

读取处理程序接收4个参数:

文件对象–具有打开的文件详细信息(权限,位置等)的每个过程结构

用户空间缓冲区

缓冲区大小

要求的位置(输入和输出参数)

要实现read回调,我们需要:

检查要求的位置

从请求的位置向用户缓冲区填充数据(最大大小<=缓冲区大小)

返回我们填充的字节数。

例如,用户运行以下代码:

int fd = open("/proc/mydev", O_RDWR);

len = read(fd,buf,100);

len = read(fd,buf,50);

在第一次调用read时,我们得到用户缓冲区,大小= 100,po​​sition = 0,我们需要从位置0开始最多填充100个字节的缓冲区,更新该位置并返回我们写入的字节数。如果我们用100个字节填充缓冲区,并在下一次读取调用时返回100,则得到用户缓冲区,大小为50,位置为100

假设我们有2个模块参数,并且我们想在proc读取处理程序中返回它们的值,我们编写以下代码:

static int irq=20;

module_param(irq,int,0660);

static int mode=1;

module_param(mode,int,0660);

static ssize_t myread(struct file *file, char __user *ubuf,size_t count, loff_t *ppos)

{

char buf[BUFSIZE];

int len=0;

printk( KERN_DEBUG "read handler\n");

if(*ppos > 0 || count < BUFSIZE)

return 0;

len += sprintf(buf,"irq = %d\n",irq);

len += sprintf(buf + len,"mode = %d\n",mode);

if(copy_to_user(ubuf,buf,len))

return -EFAULT;

*ppos = len;

return len;

}

这是一个简单的实现,我们检查这是否是第一次调用read(pos = 0),并且用户缓冲区大小大于BUFSIZE,否则返回0(文件末尾)

然后,我们构建返回的缓冲区,将其复制到用户,更新位置并返回我们编写的数字

构建并插入模块,可以使用cat命令对其进行测试:

# sudo insmod ./simproc.ko irq=32 mode=4

# cat /proc/mydev

irq = 32

mode = 4

与用户空间交换数据

在内核代码中,不能仅在用户空间提供的地址与内核空间中的缓冲区的地址之间使用memcpy:

对应于完全不同的地址空间(由于虚拟内存)。

用户空间地址可以换出到磁盘。

用户空间地址可能无效(用户空间进程试图访问未经授权的数据)。

您必须在读写文件操作代码中使用专用功能:

#include

unsigned long copy_to_user(void __user *to,const void *from, unsigned long n);

unsigned long copy_from_user(void *to,const void __user *from,unsigned long n);

实现写处理程序

写入处理程序类似于读取处理程序。唯一的区别是用户缓冲区类型是const char指针。我们需要将数据从用户缓冲区复制到请求的位置,并返回复制的字节数

在此示例中,我们想使用一个简单的命令设置两个值:

# echo "32 6" > /proc/mydev

第一个值为irq编号,第二个为模式。

写入处理程序的代码:

static ssize_t mywrite(struct file *file, const char __user *ubuf,size_t count, loff_t *ppos)

{

int num,c,i,m;

char buf[BUFSIZE];

if(*ppos > 0 || count > BUFSIZE)

return -EFAULT;

if(copy_from_user(buf,ubuf,count))

return -EFAULT;

num = sscanf(buf,"%d %d",&i,&m);

if(num != 2)

return -EFAULT;

irq = i;

mode = m;

c = strlen(buf);

*ppos = c;

return c;

}

再次,我们检查这是否是首次调用write(位置0),然后使用copy_from_user将数据从用户地址空间转移到内核地址空间。我们提取值,检查错误,更新位置并返回收到的字节数

完整的模块代码:

#include

#include

#include

#include

#include

#include

#define BUFSIZE 100

MODULE_LICENSE("Dual BSD/GPL");

MODULE_AUTHOR("Liran B.H");

static int irq=20;

module_param(irq,int,0660);

static int mode=1;

module_param(mode,int,0660);

static struct proc_dir_entry *ent;

static ssize_t mywrite(struct file *file, const char __user *ubuf, size_t count, loff_t *ppos)

{

int num,c,i,m;

char buf[BUFSIZE];

if(*ppos > 0 || count > BUFSIZE)

return -EFAULT;

if(copy_from_user(buf, ubuf, count))

return -EFAULT;

num = sscanf(buf,"%d %d",&i,&m);

if(num != 2)

return -EFAULT;

irq = i;

mode = m;

c = strlen(buf);

*ppos = c;

return c;

}

static ssize_t myread(struct file *file, char __user *ubuf,size_t count, loff_t *ppos)

{

char buf[BUFSIZE];

int len=0;

if(*ppos > 0 || count < BUFSIZE)

return 0;

len += sprintf(buf,"irq = %d\n",irq);

len += sprintf(buf + len,"mode = %d\n",mode);

if(copy_to_user(ubuf,buf,len))

return -EFAULT;

*ppos = len;

return len;

}

static struct file_operations myops =

{

.owner = THIS_MODULE,

.read = myread,

.write = mywrite,

};

static int simple_init(void)

{

ent=proc_create("mydev",0660,NULL,&myops);

printk(KERN_ALERT "hello...\n");

return 0;

}

static void simple_cleanup(void)

{

proc_remove(ent);

printk(KERN_WARNING "bye ...\n");

}

module_init(simple_init);

module_exit(simple_cleanup);

注意:要实现更复杂的proc条目,请使用 seq_file包装器

用户空间应用

您可以打开文件并使用读/写功能来测试模块。每次操作后,请不要忘记将位置基准移至0:

#include

#include

#include

#include

#include

void main(void)

{

char buf[100];

int fd = open("/proc/mydev", O_RDWR);

read(fd, buf, 100);

puts(buf);

lseek(fd, 0 , SEEK_SET);

write(fd, "33 4", 5);

lseek(fd, 0 , SEEK_SET);

read(fd, buf, 100);

puts(buf);

}

源代码:https://github.com/dev-area/proctest

或者:https://github.com/Rtoax/test/tree/master/linux/proc/proctest

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值