Skip to content
Advertisement

Unable to write on /dev/* files

I’m writing a basic char device driver for Linux kernel. For this, the code flow I have considered is as follows:

  1. alloc_chrdev_region() -> to use dynamic allocation of major number
  2. class_create() -> to create device class in sysfs
  3. device_creat() -> to create device under /dev/
  4. cdv_init() -> to initialize char device structure
  5. cdev_add() -> to add my device structure in kernel

I have added read, write, open, release methods in code.

When I try to read device file under /dev/ my read method is called. But when I try to write on /dev/ file using echo it gives error

“bash: /dev/scull: Permission denied”

I have checked permissions of file using ls -l, and I have permissions to read or write on this file.

This problem occurs for every device driver module I have written. It works well in on another machine.

I’m working on ubuntu 15.10, custom compiled kernel 4.3.0

  1. the result of ls -l /dev/scull:

    JavaScript
  2. the exact command I used to open the file

    JavaScript
  3. the source code for the open implementation

    JavaScript

Behavior I’m seeking here is, I should be able to see 'Device Written' in dmesg ouput?

Advertisement

Answer

I assume that you are normally not root on your bash shell. Then this command line

JavaScript

does not what you think. The command is executed in two steps:

  1. The bash setups the output redirection, i.e., it tries to open /dev/scull with the current user privileges.
  2. The command sudo echo 54 is executed whereas stdout is connected to the file.

As you have no write-permissions as non-root user, the first step fails and the bash reports

“bash: /dev/scull: Permission denied”

You must already be root to setup the output redirection. Thus execute

JavaScript

which gives you an interactive shell with root privileges. The you can execute

JavaScript

within that root shell.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement