Simple as that. I have the file descriptor of an opened file, and I want to know the node name of the device which contain it.
Asked
Active
Viewed 606 times
1 Answers
0
This can be made in an easy way using libudev and fstat.
#include <libudev.h> // udev headers.
#include <sys/stat.h> // for fstat function and stat struct.
#include <iostream> // for printing ouput.
#include <fcntl> // for open function.
using namespace std;
int main(int argc, char *argv[])
{
int fd = open(argv[1], O_RDONLY); // The file can be opened using any other mode, Eg. O_RDWR, O_APPEND, etc...
struct udev *udev = udev_new();
struct stat tb;
fstat(fd, &tb);
struct udev_device* dev = udev_device_new_from_devnum(udev, 'b', tb.st_dev);
cout << "The opened file is located in the device: " << udev_device_get_devnode(dev) << endl;
return 0;
}

Raydel Miranda
- 13,825
- 3
- 38
- 60
-
You don't check for errors on *any* of your function calls. This is not good example code. – nobody Jun 12 '14 at 18:17
-
1I don´t check errors for make the example shorter. I desagree about not being a good example code, since it shows the path to follow to accomplish the objective which is obtain the device node. Is not a good piece of production code, but certainly is a good example. – Raydel Miranda Jun 12 '14 at 18:37