mjl007
mjl007

Reputation: 775

can't echo to or cat from a device file that was created by mknod

I created a character device file in the /dev/ folder like this:

mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH|S_IFCHR;

const char * pathname = "/dev/tty.myown;"

int res = mknod(pathname, mode, 0);

It successfully created the character device file. So I called cat on it by calling cat /dev/tty.myown and then tried sending a message to it by running: echo "hello world" > /dev/tty.own. However the message was not displayed in the cat stream. I was wondering what other settings i need to set on that file in order to be able to read from that device file

Upvotes: 0

Views: 499

Answers (1)

nanofarad
nanofarad

Reputation: 41311

The POSIX standard (.1-2001) states:

The only portable use of mknod() is to create a FIFO-special file. If mode is not S_IFIFO or dev is not 0, the behavior of mknod() is unspecified

You are attempting to create a character-special file (i.e. not S_IFIFO). In particular, on Linux, device number 0 is a null device which ought to do absolutely nothing. If you want a FIFO, set S_IFIFO instead of S_IFCHR.

Upvotes: 2

Related Questions