Why the operator >> doesn't work with my char device?

时光毁灭记忆、已成空白 提交于 2019-12-22 18:11:50

问题


I'm currently learning linux device drivers. I have begun with an example driver, which is just a memory buffer.

My code is available on my 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.


回答1:


You need to handle O_APPEND in your write routine. The >> operator opens the file with the O_APPEND flag, which requests your driver to seek to the end before each write operation. In your case your mad_write routine should check the file flags, and seek to the end before writing if O_APPEND is set.

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. The best example I could find was in the generic file code.



来源:https://stackoverflow.com/questions/20268627/why-the-operator-doesnt-work-with-my-char-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!