2

I need to retrieved the grab state of an evdev device in a program. More specifically, I need to retrieve the state of the grab pointer in the evdev struct seen here: https://elixir.bootlin.com/linux/v4.20/source/drivers/input/evdev.c#L42 (if it is NULL or not NULL). Is this at all possible from userspace, e.g. through reading kernel memory with /dev/(k)mem or do I need to write a kernel module? Where should I get started?

2

To determine whether a device is currently grabbed, from userspace, you can try to grab it yourself; either using the EVIOCGRAB ioctl yourself, or libevdev_grab in libevdev:

if (!ioctl(evdevfd, EVIOCGRAB, (void *) 1)) {
    // We grabbed the device, no one else had it; release it
    ioctl(evdevfd, EVIOCGRAB, (void *) 0);
} else if (errno == EBUSY) {
    // The device is busy, someone else has it
} else {
    // Something else went wrong
}

or

struct libevdev *dev;
libevdev_new_from_fd(evdevfd, &dev);
if (!libevdev_grab(dev, LIBEVDEV_GRAB)) {
    // We grabbed the device, no one else had it; release it
    libevdev_grab(dev, LIBEVDEV_UNGRAB);
} else if (errno == EBUSY) {
    // The device is busy, someone else has it
} else {
    // Something else went wrong
}

(Sprinkle liberally with error-handling; evdevfd is the open file descriptor for the evdev device you’re interested in.)

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.