1

I have an input class device (accelerometer) bound to a driver which exposes sysfs attributes that I need to access from userland. Using a udev rule, I have provided an alias that unambiguously identifies my device, i.e. ll /dev/input yields:

lrwxrwxrwx    1 root     root             6 May 18 13:47 accelerometer0 -> event0
drwxr-xr-x    2 root     root            60 May 18 13:47 by-path
crw-rw----    1 root     input      13,  64 May 18 13:47 event0
crw-rw----    1 root     input      13,  65 May 18 13:47 event1
crw-rw----    1 root     input      13,  66 May 18 13:47 event2

So far, so good: I can open("/dev/input/accelerometer0") in my userland code and start streaming the data. But to, for example, change the data rate, I need to write to the pollrate_ms attribute in the related sysfs directory. An ls /sys/class/input/ yields:

event0  event1  event2  input0  input1  input2

I happen to know that pollrate_ms resides in input0/device, but I need to figure this out programatically, especially since future updates might cause that numbering to change.

I know that I can use libudev to enumerate /sys/class/input and then use a for loop to explore each of those directories until I find the one containing pollrate_ms. It just seems like that's a lot of work compared to how easy it was to unambiguously identify the device via a udev rule.

Am I missing out on an easy way or should I just suck it up?

0

Programmatically, you could first stat("/dev/input/accelerometer0", &stat_struct) to find out the major and minor device numbers.

Then use libudev's udev_device_new_from_devnum() to get a struct udev_device for your accelerometer, and then udev_device_get_syspath() to get the pathname of its sysfs directory.

  • This did the trick. The resulting syspath wasn't quite there but it was close enough (../device) and much easier than an enumerate for loop. – David H Olson May 18 '18 at 19:28

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.