[英]How to use a created Linux Kernel character driver from the commandline
I've been following the tutorial on Linux Kernel programming over here: http://www.tldp.org/LDP/lkmpg/2.6/html/index.html我一直在关注有关 Linux 内核编程的教程: http : //www.tldp.org/LDP/lkmpg/2.6/html/index.html
I've gotten to the section that is dedicated to "character device drivers" and while I've gotten it to compile, it will not function on the described case:我已经进入了专用于“字符设备驱动程序”的部分,虽然我已经编译了它,但它不会在所描述的情况下运行:
"Called when a process writes to dev file: echo "hi" > /dev/chardev" “当进程写入开发文件时调用:echo "hi" > /dev/chardev”
I've tried several Linux console commands such as:我尝试了几个 Linux 控制台命令,例如:
echo "hi" > sudo /dev/chardev/回声“嗨”> sudo /dev/chardev/
and和
sudo sh -c 'printf "hi" > sudo /dev/chardev/' sudo sh -c 'printf "hi" > sudo /dev/chardev/'
I'm running my code on a Raspberry Pi 3 B+我在 Raspberry Pi 3 B+ 上运行我的代码
When I run the first command I will get nothing in return, and nothing is added to /var/logs/messages当我运行第一个命令时,我不会得到任何回报,也不会向 /var/logs/messages 添加任何内容
When I run the second command I get: sh:printf: I/O error当我运行第二个命令时,我得到: sh:printf: I/O 错误
Full code over at: http://www.tldp.org/LDP/lkmpg/2.6/html/x569.html完整代码在: http : //www.tldp.org/LDP/lkmpg/2.6/html/x569.html
I've modified the code with my snippet below.我用下面的代码段修改了代码。
/*
* Called when a process writes to dev file: echo "hi" > /dev/chardev
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
printk(KERN_INFO "%s\n", buff);
return -EINVAL;
}
What I'm expecting to happen is when I use echo "hi" > sudo /dev/chardev that in my /var/logs/messages a line will appear that simply says "hi".我期望发生的是当我使用 echo "hi" > sudo /dev/chardev 时,在我的 /var/logs/messages 中会出现一行简单地说“hi”。
echo "hi" > /dev/chardev
回声“嗨”> /dev/chardev
This is ok.还行吧。
echo "hi" > sudo /dev/chardev/
回声“嗨”> sudo /dev/chardev/
This is invalid.这是无效的。 This will
echo hi /dev/chardev/
and write that to file named sudo
.这将
echo hi /dev/chardev/
并将其写入名为sudo
文件。 And don't /dev/chardev/
, it't not a directory, it's a file, it's /dev/chardev
(without the /
on the end).并且不要
/dev/chardev/
,它不是目录,而是文件,它是/dev/chardev
(最后没有/
)。
sudo sh -c 'printf "hi" > sudo /dev/chardev/'
sudo sh -c 'printf "hi" > sudo /dev/chardev/'
Same error as above.和上面一样的错误。
If you want to append to a file using sudo, use tee
, as in echo hi | sudo tee /dev/chardev
如果要使用 sudo 附加到文件,请使用
tee
,如echo hi | sudo tee /dev/chardev
echo hi | sudo tee /dev/chardev
. echo hi | sudo tee /dev/chardev
。 Or if you have to sudo sh -c 'echo "hi" > /dev/chardev'
.或者,如果您必须
sudo sh -c 'echo "hi" > /dev/chardev'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.