As other users have already explained in great detail, special files require code to back them up. However, nobody seems to have mentioned that Linux provides several ways to write that code in userspace:
A. FUSE (Filesystem in USErspace) allows you to write something like /proc
without risk of crashing the kernel and do it in a language/runtime of your choice, such as Go, Node.js, Perl, PHP, Python, Ruby, Rust, etc..
It also has the advantage that FUSE filesystems can be mounted without sudo
because they run as the user doing the mounting.
Here are some examples of things people have written using FUSE:
- mp3fs (View your FLAC files as MP3 files that get created on-the-fly when you copy/click-drag them to your MP3 player)
- PyTagsFS (View your media in a tree of virtual folders built from the metadata tags)
- fuse-zip (Mount Zip files as folders)
- FuseISO (Mount ISOs without root permissions)
- iFUSE (Mount iDevices)
- FuseDAV (Mount WebDAV shares)
- fuse-exfat (Mount exFAT-formatted filesystems)
- ntfs-3g (The Linux NTFS driver)
B. If you want to create a virtual input device like a keyboard, mouse, joystick, etc. (eg. to write a userspace driver for a USB device you're talking to using libusb
), there's uinput.
Bindings for it are harder to find, but I know they exist for Go (Keyboard-only), Python, and Ruby (2).
Examples of real-world uinput use include:
- G15Daemon (Linux driver for the LCD and gaming keys on Logitech G15 gaming keyboards)
- ds4drv (Driver for Sony DualShock 4 controllers)
- xboxdrv (Alternative XBox 360 controller driver and Linux equivalent to x360ce so badly designed games like Runner2: Future Legend of Rhythm Alien can think they're talking to a real XBox controller when they're not)
- The old Wiimote drivers like cwiid that were required before someone finally wrote a kernel Wiimote driver so support would be available by default.
C. For generic character devices, there's CUSE (Character devices in USErspace). It's much less popular though.
The only user of the CUSE API that I'm personally aware of is the same program which prompted its creation: osspd, which implements /dev/dsp
, /dev/adsp
, and /dev/mixer
(the OSS audio API) in userspace so they can be routed through PulseAudio or dmix.
The only CUSE binding I was able to find is cusepy, which hasn't been updated since 2010.
D. You may not need a new special file at all.
For example, you can open up raw communication with any USB device using libusb (List of bindings on the page) and then communicate with other programs through some other mechanism (TCP/UDP sockets, reading/writing stdin/stdout or regular files on disk, etc.).
man 2 mknod
open()
,read()
,close()
, etc. After that, it's up to the software