Direct interaction with a block device requires a device node. The device node controls permissions (who can access the device directly).
Theoretical solution: call mknod
or mknodat
to create a block device (S_IFBLK
) with the given dev_t
value. It needs to be on a filesystem that supports device files.
mknod("/var/run/myprogram/mydevice", S_IFBLK | S_IRUSR | S_IWUSR, buf.st_dev);
This requires elevated privileges: classically root permissions. On Linux, the CAP_MKNOD
capability is sufficient, but some security frameworks may prevent even root from creating device nodes in random places. I don't know whether this requires more than root permissions on macOS.
Make sure not to give unprivileged programs access to the device.
Practical solution: look under /dev
for a suitable device, i.e. a block device ((found_stat.st_mode & S_IFBLK) != 0
) such that found_stat.st_rdev == buf.st_dev
. There's no POSIX C function that does the recursive search, so you'll have to code it manually.
While find
can do the recursive traversal, it has no option to match a device file by its device number, which makes it inconvenient to locate the desired device.