什么是copy_from_user的简单示例

问题描述

已经有关于InvalidInputException的{​​{3}},但是对我来说有点太先进了,我想看看在内核模块中使用Exception方法的简单示例,是使用该方法的最简单示例?

 //Using directives
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 //Attribute
 [DoNotRename] //<--------- (Above the class but below the usin directives)

 //Class
 public class Class_Name: MonoBehavIoUr
 {
 
 }

解决方法

使用copy_from_user的经典示例是实现字符设备的write功能。假设您有一个注册character device的模块,该模块接收来自用户的文件路径。其代码将类似于:

static ssize_t device_write(struct file *fs,const char *buffer,size_t len,loff_t *offset)
{
    char* file_path = (char*)kmalloc(len,GFP_KERNEL);
    if (NULL == file_path) {
        return -ENOMEM;
    }

    copy_from_user(file_path,buffer,len);
    file_path[len - 1] = 0x00;
    
    return len;
}

通常,copy_from_usermemcpy相同,但是只有它期望将用户空间缓冲区作为目的地。