[英]Why the operator >> doesn't work with my char device?
I'm currently learning linux device drivers. 我目前正在学习linux设备驱动程序。 I have begun with an example driver, which is just a memory buffer.
我从一个示例驱动程序开始,它只是一个内存缓冲区。
My code is available on my github . 我的代码可以在我的github上找到 。
I test my driver by doing this: 我这样做来测试我的驱动程序:
# echo "Hello World" > /dev/mad
# cat /dev/mad
Hello World
This is going well but when I use the redirection operator to append something (>>), the behaviour is not the one that I expected. 这进展顺利,但是当我使用重定向运算符添加某些内容(>>)时,其行为并非我所期望的。
# echo foo > /dev/mad
# echo bar >> /dev/mad
# cat /dev/mad
bar
I expected rather to have: 我希望有:
foo
bar
I have implemented the llseek
callback and take care of the offp
in the read
and write
callbacks, but it still doesn't work. 我已经实现了
llseek
回调,并在read
和write
回调中处理了offp
,但仍然无法正常工作。
You need to handle O_APPEND
in your write routine. 您需要在写入例程中处理
O_APPEND
。 The >>
operator opens the file with the O_APPEND
flag, which requests your driver to seek to the end before each write operation. >>
运算符使用O_APPEND
标志打开文件,该标志要求您的驱动程序在每次写操作之前搜索到结尾。 In your case your mad_write
routine should check the file flags, and seek to the end before writing if O_APPEND
is set. 在您的情况下,您的
mad_write
例程应检查文件标志,并在写入之前(如果已设置O_APPEND
话)查找结尾。
See the manual definition here . 请参阅此处的手册定义 。 I had a look around the Linux kernel source for examples, but very few character drivers actually handle
O_APPEND
. 我以Linux内核源代码为例,但实际上很少有字符驱动程序可以处理
O_APPEND
。 The best example I could find was in the generic file code . 我能找到的最佳示例是通用文件代码 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.