C/C++教程

compat_ioctl和unlocked_ioctl的转换问题

本文主要是介绍compat_ioctl和unlocked_ioctl的转换问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <linux/compat.h>   //否则报compat_alloc_user_space找不到

//compact_ioctl中先对arg做些处理,然后直接调用ioctl即可

long compact_ioctl(struct file *file, unsigned int cmd, unsigned long arg){
	compat_uptr_t karg[4];
	unsigned long __user *ubuffer;

	if (copy_from_user((void *)karg, (void __user *)arg, 4 * sizeof(compat_uptr_t))) {
		printk("copy_from_user fail\n");
		return -EFAULT;
	}

	ubuffer = compat_alloc_user_space(4 * sizeof(unsigned long));
	if (!access_ok(VERIFY_WRITE, ubuffer, 4 * sizeof(unsigned long)))
		return -EFAULT;

	if (put_user(karg[0], &ubuffer[0]) ||
	    put_user(karg[1], &ubuffer[1]) ||
	    put_user(karg[2], &ubuffer[2]) || 
		put_user(karg[3], &ubuffer[3])) {
		printk("put_user fail\n");
		return -EFAULT;
	}

	return ioctl(file, cmd, (unsigned long)ubuffer);
}

  

这篇关于compat_ioctl和unlocked_ioctl的转换问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!