在内核空间打印字符数组信息内容

头文件:

#ifndef __MYLED_H__
#define __MYLED_H__
#define UACCESS_BUF _IOW('a',1,char [128])

#endif

内核程序:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"

#define CNAME "myled"

int major;
char kbuf[128] = {0};

int myled_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t myled_read (struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //1.校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,需要更正大小
    if(size > sizeof(kbuf)) size = sizeof(kbuf);
    //2.将数据从用户空间拷贝到内核空间
    ret = copy_to_user(ubuf,kbuf,size);
    if(ret) //3.判断是否错误
    {
        printk("copy to user is error\n");
        return -EIO;
    }
    return size; //5.返回拷贝数据大小
}
ssize_t myled_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //1.校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,需要更正大小
    if(size > sizeof(kbuf)) size = sizeof(kbuf);
    //2.将数据从用户空间拷贝到内核空间
    ret = copy_from_user(kbuf,ubuf,size);
    if(ret) //3.判断是否错误
    {
        printk("copy from user is error\n");
        return -EIO;
    }
    //4.打印传递数据内容
    printk("copy from user kbuf:%s\n",kbuf);
    //kbuf[0]:代表操作的是那一盏灯,kbuf[0] = 0  kbuf[0] = 1 kbuf[0] = 2
    //kbuf[1]:代表led灯的状态   kbuf[1] = 0  kbuf[1] = 1
    return size; //5.返回拷贝数据大小
}

long myled_ioctl (struct file *file, unsigned int cmd, unsigned long args)
{

    //1.判断cmd switch(cmd)
    //2.判断操作哪盏灯进行点亮 copy_from_user
    char whitch[128];
    int ret;
    
    ret = copy_from_user(whitch,(void*)args,sizeof(whitch));
    if(ret)
    {
        printk("copy from user is error\n");
        return -EIO;
    }
    printk("%s\n",whitch);
        
    return 0;
}
int myled_close (struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

const struct file_operations fops = {
    .open = myled_open,
    .read = myled_read,
    .write = myled_write,
    .unlocked_ioctl = myled_ioctl,
    .release = myled_close,
};
static int __init mycdev_init(void)
{
    //1.注册字符设备驱动
    major = register_chrdev(0,CNAME,&fops);
    if(major < 0) //2.判断返回值
    {
        printk("register chrdev is error\n");
    }
    //3.打印主设备号
    printk("register chrdev major=%d\n",major);

    return 0;
}

static void __exit mycdev_exit(void)
{
    //1.注销字符设备驱动
    unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

测试文件:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include "myled.h"

int main(int argc,const char * argv[])
{
    char buf[128] = "hello world\n";
    int fd = -1;
    fd = open("/dev/myled",O_RDWR);
    if(fd == -1)
    {
        perror("open is error\n");
        return -1;
    }
    ioctl(fd,UACCESS_BUF,buf);

    return 0;
}

工程管理文件:

ARCH ?=x86
modname ?= mycdev
ifeq ($(ARCH),arm)
#定义一个变量,存放linux内核源码目录,arm架构
KERNEDIR:=/home/ubuntu/linux-5.10.61
#x86架构
else
KERNEDIR:=/lib/modules/$(shell uname -r)/build
#定义一个变量,开启一个终端,执行pwd命令
endif
PWD:=$(shell pwd)

all:
	@#-C:跳转到内核顶层目录下,读取内核顶层目录下的Makefile文件
	@#在内核源码顶层目录下执行:make M=$(shell pwd) modules
	@#M=$(shell pwd):回到当前目录下,只编译当前目录下的文件
	@#make modules:采用模块化方式进行编译
	make -C $(KERNEDIR) M=$(shell pwd) modules

clean:
	make -C $(KERNEDIR) M=$(shell pwd) clean

#指定模块化方式编译的文件
obj-m:=$(modname).o

运行效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用数组通过ioctl将内核空间信息传递到用户空间的流程如下: 1. 在驱动程序中定义ioctl命令,其中包含需要传递的信息。例如: ```c #define IOCTL_GET_ARRAY _IOR('x', 1, int[MAX_ARRAY_SIZE]) ``` 其中,_IOR代表是从设备中读取信息,'x'代表一个唯一的驱动程序标识符,1代表命令号,int[MAX_ARRAY_SIZE]代表需要传递的整型数组。 2. 在驱动程序中实现ioctl函数,处理用户空间传来的ioctl命令。例如: ```c static long my_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) { int array[MAX_ARRAY_SIZE]; switch (cmd) { case IOCTL_GET_ARRAY: /* 将内核空间数组复制到array数组中 */ memcpy(array, kernel_array, sizeof(int) * MAX_ARRAY_SIZE); /* 将array数组传递给用户空间 */ if (copy_to_user((void __user *)arg, array, sizeof(int) * MAX_ARRAY_SIZE)) return -EFAULT; break; default: return -ENOTTY; } return 0; } ``` 其中,MAX_ARRAY_SIZE是数组的大小,kernel_array是内核空间数组。 3. 在用户空间中使用ioctl命令读取内核空间数组。例如: ```c int fd; int array[MAX_ARRAY_SIZE]; fd = open("/dev/my_device", O_RDONLY); /* 读取内核空间数组 */ if (ioctl(fd, IOCTL_GET_ARRAY, array)) return -1; /* 使用读取到的数组 */ for (int i = 0; i < MAX_ARRAY_SIZE; i++) { printf("array[%d]: %d\n", i, array[i]); } close(fd); ``` 其中,fd是设备文件描述符,/dev/my_device是设备节点路径,IOCTL_GET_ARRAY是ioctl命令,array是接收数组的指针。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值