C:尝试在Linux中创建角色设备

时间:2015-07-02 14:58:20

标签: c ubuntu linux-kernel linux-device-driver

我正在尝试在Linux中创建一个能够读/写用户提供给缓冲区的字符设备。我到目前为止:

static ssize_t device_read(struct file *file, 
           char __user * buffer, /* buffer to be filled with data */
           size_t length,  /* length of the buffer     */
           loff_t * offset)
{
int i,getout;
getout = 0;

for(i = bufferindex; i < length; i++)
{
    if(put_user(Message[bufferindex],buffer) != 0)
    {
        printk("put_user failed\n");
        return -EFAULT;
    }
    bufferindex ++;
    if(bufferindex == strlen(Message))
    {
        getout = 1;
        bufferindex = 0;
    }
    if(getout)
        break;
}


return i;
}

 static ssize_t
 device_write(struct file *file,
     const char __user * buffer, size_t length, loff_t * offset)
{ 
int i;
bufferindex = 0;
printk("device_write(%p,%d)\n", file, (int)length);

for (i = 0; i < length && i < BUF_LEN-1; i++)
    if(get_user(Message[i], buffer + i) != 0)
    {
        printk("get_user failed\n");
        return -EFAULT;
    }

Message[i+1] = '\0';
/* return the number of input characters used */
return i;
}

如何测试它是否正常工作(我加载了驱动程序并使用了mknod,但我如何读/写并检查它是否正常工作?)另外,如何检测用户试图让我从腐败中写入?缓冲

欢迎任何帮助!

0 个答案:

没有答案