Skip to content

Commit

Permalink
Warn if opening a device failed (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1993 authored Sep 15, 2023
1 parent 2945794 commit 24f0422
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Server.vala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,30 @@ namespace Evdevhook {
IOChannel iochan;
{
int fd = Posix.open(udevdev.devnode, Posix.O_RDONLY | Posix.O_NONBLOCK);
if (fd == -1) return;
var saved_errno = errno;
if (fd == -1) {
string? devname = null;

// Search up the device tree until name is found or we leave input subsystem
for(unowned var? parent = udevdev;
parent != null && devname == null;
parent = parent.get_parent_with_subsystem_devtype("input", null)
) {
devname = parent.get_sysattr_value("name");
}

if (devname != null) {
printerr("\nFailed to open %s (%s) - %s\n", devname, udevdev.devnode, strerror(saved_errno));
} else {
printerr("\nFailed to open %s - %s\n", udevdev.devnode, strerror(saved_errno));
}

if (saved_errno == Posix.EACCES) {
printerr("The recommended way to fix this is by creating an appropriate udev rule and/or adding your user to a correct group.\n");
printerr("Running as root also works, but is not advised.\n");
}
return;
}

iochan = new IOChannel.unix_new(fd);
iochan.set_close_on_unref(true);
Expand Down
2 changes: 2 additions & 0 deletions vapi/libudev.vapi
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace Udev {
public ListEntry? tags_list_entry { get; }

public unowned string? get_property_value(string key);
public unowned string? get_sysattr_value(string key);
public unowned Device? get_parent_with_subsystem_devtype(string? subsystem, string? devtype);
}

[CCode (cname = "struct udev_enumerate", ref_function = "udev_enumerate_ref", unref_function = "udev_enumerate_unref")]
Expand Down

0 comments on commit 24f0422

Please sign in to comment.