Linux – Create a general character devices pair

devicelinux

I'd like to create a character device which has the same properties as a standard pseudo-terminal, but that can be named in a specific name.

Basically I'd like to have /dev/pts/my-unique-name instead of the numbers which can be reused. Is there a way to do that? Can mknod for example create arbitrarily connected char-devices?

Best Answer

mknod will work, with some exceptions. The syntax is:

mknod /path/to/new/dev c major minor

So, for example, you should likely be able to create a new pts type device with

mknod /tmp/mypts c 136 0

What I've found, however, is that if you try to do this in /dev/pts, you will get an access denied message. I can do it in /dev, just not /dev/pts. I'm on a Centos 5.5 box. YMMV.

This is because /dev/pts is mounted by the kernel (from my /etc/fstab file):

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0

This is a kernel-managed pseudo filesystem, and I wouldn't think that screwing around with it is a good idea. The standard way to get a new file created in there is by open()ing /dev/ptmx; that will get the calling process the fd of the master terminal assigned, and a new device will be created as /dev/pts/X where X is dynamically allocated by ptmx as the slave. This would then be opened itself, usually by a forked process from the original one.

There's likely a good reason why it's done this way. I'm not sure what it is, but I would avoid trying to break it, if system stability is something you value.

That all being said, the first command line with real options I presented would allow you to make your own pts device anywhere but /dev/pts, and depending on what you're intending to do with it, maybe that's enough to get you where you're going.