I'm having an issue where a 3rd party application is opening a device node it shouldn't.

I wrote a SPI Slave device driver that does some processing of messages. When the driver loads its in "Initialize Mode" which only replies to a specific message. To re-initialize communications I must unload and reload the driver. The problem is that the system has a 3rd party application, running as root, that opens all character devices. The only way to make this all work is to stop the app and unload which causes a huge problem with the over all project.

Is it possible to open a connection to a Character Device without actually having a device node? Right now I use fopen('/dev/spi') and everything works fine until I need to reload and the above issue ensues. Is there any sort of system call that can be made that would result in a file descriptor to a specific Major/Minor node number without having the actual /dev/spi file?

  • I don't think you can do it without a device node per se, but you can define which and where based on maj:min or whatever with udev rules. It may also be possible to mount --bind over that /dev but I'm not sure if it would still be accessible to you after - probably that could be handled with mount namespaces though. – mikeserv Jun 19 '14 at 21:34
  • That application seems obnoxious. Can you run it chrooted? – Gilles Jun 19 '14 at 22:45

If the offending application only opens device nodes in the /dev tree then you could place your device node somewhere else, if that works with your own program.

The filesystem the device node lives on have to have the mount option dev set for it to have special treatment of device node files as such.

(Disclaimer: I have not tested this, but I have done similar things before...)

One example would be to mount a small tmpfs filesystem on, for example, /my-dev/ and create the device node there.

mkdir -p /my-dev

mount -t tmpfs -o dev,size=<size> tmpfs /my-dev

mknod /my-dev/spi <type> <maj> <min>

where <size> is the size of the tmpfs, <type> is the device node type and <maj> <min> are the major:minor device numbers; see the man-pages for mount and mknod respectively.

...And then use fopen("/my-dev/spi") in your program.

(And, of course, make sure that udev or similar does not make a node for the device.)

  • I've tried moving it to different places in my root directory with no change. I'll have to try mounting a new fs like you suggested. – jecxjo Jun 19 '14 at 22:21
  • @jecxjo Do you know the mechanism the offending program uses for enumerating the devices? Does it just check everything in /dev or does it use some hook in udev or somewhere else (in the kernel, perhaps) or does it have some other way? – Johan E Jun 19 '14 at 22:27
  • no idea, its a proprietary app that uses a 485 interface to a radio. Think they put in code to "auto detect" character devices. Cant change this code, just have to live with it. – jecxjo Jun 19 '14 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.