gpt4 book ai didi

c - 无法发现 echo 内核模块中的错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:44 24 4
gpt4 key购买 nike

我正在尝试编写一个非常简单的 echo Linux 驱动程序。

驱动程序从命令行获取最多 250 个字符,并将其写入虚拟设备“mydev”。这再次从设备中读回。下面粘贴前端和驱动代码以供引用。

问题是我能写但不能读。编译或段错误没有错误。但是驱动程序读取的 printk 中的消息都没有被打印出来。我对发生的事情感到困惑。我可以在这里得到一些线索吗?

我只是为了更清楚地共享代码副本:

mydriver.c :

    #include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

MODULE_LICENSE("GPL");

static int major;
static char kbuf[250];

static int dv_open(struct inode *inode, struct file *filp)
{
return 0;
}

static int dv_close(struct inode *inode, struct file *filp)
{
return 0;
}

static ssize_t dv_read(struct file *filp, char __user *buf,
size_t sz, loff_t *fpos)
{
int r;
int L;
printk("READ:Entering\n");

L = strlen(kbuf);
r = copy_to_user(buf, kbuf, L);

printk("READ:Ends\n");

return L;
}
static ssize_t dv_write(struct file *filp, const char __user *buf,
size_t sz, loff_t *fpos)
{
int r, wr_sz;

printk("WRITE:Entering\n");
memset(kbuf,'\0', 250);
if ( sz <= 250 ) {
wr_sz = sz;
} else {
wr_sz = 250;
}
r = copy_from_user(kbuf, buf, wr_sz);

printk("WRITE:Rx buf = %s\n", kbuf);

return 0;

}
static struct file_operations dv_fops = {
.open = dv_open,
.release = dv_close,
.read = dv_read,
.write = dv_write,
.owner = THIS_MODULE,
};

int init_module(void)
{
major = register_chrdev(0, "dvdev", &dv_fops);
if ( major < 0 ) {
printk("Error in registering driver\n");
return -1;
}
else printk("Success. major = %d\n", major);
return 0;
}

void cleanup_module(void)
{
unregister_chrdev(major, "dvdev");
}

myuserapp.c

    #include <stdio.h>
#include <fcntl.h>
#include <string.h>

static char buf[250];
static char * wbuf;
int main(int argc, char **argv)
{
int fd;
int option;
int nbr = 0, len;

if ( argc != 2 ) {
printf("usage: front <devName>\n");
return -1;
}
fd = open("mydev", O_RDONLY | O_WRONLY);
if ( fd < 0 ) {
printf("Error opening file. %s does not exist\n", argv[1]);
return -2;
}

wbuf = argv[1];
len = strlen(wbuf);
nbr = write(fd, wbuf, len);
printf("USR: Buf written = %s, nbr = %d\n", wbuf ,nbr);


nbr = read(fd, buf, 250);
printf("USR RD: %s", buf);

close(fd);
return 0;
}

最佳答案

您的代码至少有一个错误:

 fd = open("mydev", O_RDONLY | O_WRONLY);

这是一个不正确的 open() 调用。
man page for open()指定:

Applications shall specify exactly one of the first three values (file access modes) below in the value of oflag:

O_RDONLY Open for reading only.
O_WRONLY Open for writing only.
O_RDWR Open for reading and writing. The result is undefined if this flag is applied to a FIFO.

您有两个值的表达式,而不是只指定一个值。

I guess O_RDONLY and O_WRONLY are bits individually, so it would be something like O_RDONLY | O_WRONLY = 10|01 = 11 . Both the bits of Read and write are set.

位值无关紧要,因为不允许组合这些值。
您似乎忽略了排他性后缀“ONLY”。
RDONLY 表示“允许读取和禁止写入”。
WRONLY 表示“允许写入和禁止读取”。
“O_RDONLY | O_WRONLY”是一个逻辑矛盾。
如果要同时允许读和写,则必须指定 O_RDWR。

并且 Mark Stevens 已经提供了正确的值和 bool 运算来证明你的错误表达式不等同于 O_RDWR。

关于c - 无法发现 echo 内核模块中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15623911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com