简体   繁体   中英

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

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"

I've tried several Linux console commands such as:

echo "hi" > sudo /dev/chardev/

and

sudo sh -c 'printf "hi" > sudo /dev/chardev/'

I'm running my code on a Raspberry Pi 3 B+

When I run the first command I will get nothing in return, and nothing is added to /var/logs/messages

When I run the second command I get: sh:printf: I/O error

Full code over at: 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" > /dev/chardev

This is ok.

echo "hi" > sudo /dev/chardev/

This is invalid. This will echo hi /dev/chardev/ and write that to file named sudo . And don't /dev/chardev/ , it't not a directory, it's a file, it's /dev/chardev (without the / on the end).

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 echo hi | sudo tee /dev/chardev . Or if you have to sudo sh -c 'echo "hi" > /dev/chardev' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM