ioctl练习题:传递字符数组/传递结构体

传递字符数组要求:

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

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

传递结构体要求:

typedef struct{ int width; int high;}image_t;用户空间: width = 10 high = 200将用户空间数据传递给内核空间,在内核空间进行打印,打印之后需要每个变量+10,在传递给用户空间,在用户空间打印增加后的值

mycdev.c代码如下:

#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);

 
    return size; //5.返回拷贝数据大小
}

long myled_ioctl (struct file *file, unsigned int cmd, unsigned long args)
{
    int ret;
    image_t img;
    //判断cmd
 
    switch(cmd)
    {
        case UACCESS_BUF:
        ret = copy_from_user(kbuf,(void*)args,sizeof(kbuf));
        if(ret)
        {
            printk("copy from user is error\n");
            return -EIO;
        }
        printk("字符数组信息内容:%s\n",kbuf);
        break;

        case UACCESS_STRUCT:
        ret = copy_from_user(&img,(void*)args,sizeof(img));
        if(ret)
        {
            printk("copy from user is error\n");
            return -EIO;
        }
        printk("width = %d high = %d",img.width,img.high);
        img.width += 10;
        img.high += 10;
        ret = copy_to_user((void*)args,&img,sizeof(img));
        if(ret)
        {
            printk("copy to user is error\n");
            return -EIO;
        }
        break;

    }
    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 = {
    .unlocked_ioctl = myled_ioctl,
};
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)
{
    unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

text.c

#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] = {0};
    int fd = -1;
    image_t img = {
        .width = 10,
        .high = 210,    
    };
    fd = open("/dev/myled",O_RDWR);
    if(fd == -1)
    {
        perror("open is error\n");
        return -1;
    }
    while(1)
    {
    fgets(buf,sizeof(buf),stdin);
    buf[strlen(buf)-1] = '\0';

    ioctl(fd,UACCESS_BUF,buf);

    ioctl(fd,UACCESS_STRUCT,&img);
    printf("width = %d high = %d",img.width,img.high);
    }
    close(fd);
    return 0;
}

.h

#ifndef __MYLED_H__
#define __MYLED_H__

typedef struct{
    int width;
    int high;
}image_t;

#define UACCESS_BUF _IOW('a',1,char[128])
#define UACCESS_STRUCT _IOW('a',2,image_t)
#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值