Linux kernel “回声”;小号&燃气轮机;杂项字符驱动程序-被截断
我已经编写了一个misc char驱动程序,当我将驱动程序的次要编号回显到misc char驱动程序时,它不会收到第一个数字 写函数是这样的Linux kernel “回声”;小号&燃气轮机;杂项字符驱动程序-被截断,linux-kernel,linux-device-driver,Linux Kernel,Linux Device Driver,我已经编写了一个misc char驱动程序,当我将驱动程序的次要编号回显到misc char驱动程序时,它不会收到第一个数字 写函数是这样的 static ssize_t hellowld_write(struct file *file, const char *buf,size_t len, loff_t *ppos) { pr_debug("In hellowld_write len %d buf %s \n",len,buf); minor
static ssize_t hellowld_write(struct file *file, const char *buf,size_t len,
loff_t *ppos)
{
pr_debug("In hellowld_write len %d buf %s \n",len,buf);
minor[0] = '\0';
snprintf(minor, 20,"%d", hellowld_device.minor);
strncpy(inputminor, buf,len);
pr_debug("In hellowld_write -- minor %s -- recvNum %s end\n",minor, inputminor);
pr_debug("In hellowld_write -- strlen minor %d -- strlen inputminor %d end\n", strlen(minor), strlen(inputminor));
if( strncmp(minor, inputminor, strlen(minor)) == 0)
{
pr_debug("In hellowld_write -- strcmp passed \n");
return strlen(minor);
}
else
{
pr_debug("In hellowld_write -- strcmp failed \n");
return -EFAULT;
}
}
当我这样做的时候,
echo“54”>/dev/hellowld
Dec 31 11:36:40 pavan-linux kernel: [16981.382424] In hellowld_write len 1 buf
Dec 31 11:36:40 pavan-linux kernel: [16981.382424]
Dec 31 11:36:40 pavan-linux kernel: [16981.382424] o
Dec 31 11:36:40 pavan-linux kernel: [16981.382437] In hellowld_write -- minor 54 -- recvNum
Dec 31 11:36:40 pavan-linux kernel: [16981.382437] 4
Dec 31 11:36:40 pavan-linux kernel: [16981.382437] end
Dec 31 11:36:40 pavan-linux kernel: [16981.382444] In hellowld_write -- strlen minor 2 -- strlen inputminor 3 end
Dec 31 11:36:40 pavan-linux kernel: [16981.382448] In hellowld_write -- strcmp failed
Dec 31 11:36:52 pavan-linux kernel: [16993.356993] In hellowld_write len 3 buf 22
Dec 31 11:36:52 pavan-linux kernel: [16993.356993]
Dec 31 11:36:52 pavan-linux kernel: [16993.356993] o
Dec 31 11:36:52 pavan-linux kernel: [16993.357011] In hellowld_write -- minor 54 -- recvNum 22
Dec 31 11:36:52 pavan-linux kernel: [16993.357011] end
Dec 31 11:36:52 pavan-linux kernel: [16993.357017] In hellowld_write -- strlen minor 2 -- strlen inputminor 3 end
Dec 31 11:36:52 pavan-linux kernel: [16993.357021] In hellowld_write -- strcmp failed
echo“22”>/dev/hellowld
Dec 31 11:36:40 pavan-linux kernel: [16981.382424] In hellowld_write len 1 buf
Dec 31 11:36:40 pavan-linux kernel: [16981.382424]
Dec 31 11:36:40 pavan-linux kernel: [16981.382424] o
Dec 31 11:36:40 pavan-linux kernel: [16981.382437] In hellowld_write -- minor 54 -- recvNum
Dec 31 11:36:40 pavan-linux kernel: [16981.382437] 4
Dec 31 11:36:40 pavan-linux kernel: [16981.382437] end
Dec 31 11:36:40 pavan-linux kernel: [16981.382444] In hellowld_write -- strlen minor 2 -- strlen inputminor 3 end
Dec 31 11:36:40 pavan-linux kernel: [16981.382448] In hellowld_write -- strcmp failed
Dec 31 11:36:52 pavan-linux kernel: [16993.356993] In hellowld_write len 3 buf 22
Dec 31 11:36:52 pavan-linux kernel: [16993.356993]
Dec 31 11:36:52 pavan-linux kernel: [16993.356993] o
Dec 31 11:36:52 pavan-linux kernel: [16993.357011] In hellowld_write -- minor 54 -- recvNum 22
Dec 31 11:36:52 pavan-linux kernel: [16993.357011] end
Dec 31 11:36:52 pavan-linux kernel: [16993.357017] In hellowld_write -- strlen minor 2 -- strlen inputminor 3 end
Dec 31 11:36:52 pavan-linux kernel: [16993.357021] In hellowld_write -- strcmp failed
为什么当我回显次要数字时,hellowld_write中接收到的数据会被截断?您的函数
hellowld_write
做了两件坏事:它直接访问buf
(不从用户copy_
),并且不返回值。请在测试中使用此函数的实现:pr_debug(“在hellowld_write len%d\n中”,len);回程透镜代码>。您能发布完整的驱动程序代码吗?Tsyvarev的评论很有帮助