问题
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