diff options
author | Hans de Goede <hdegoede@redhat.com> | 2013-05-24 15:52:45 +0200 |
---|---|---|
committer | Hans de Goede <hdegoede@redhat.com> | 2013-05-30 14:20:17 +0200 |
commit | d250964e64c97d92460c96fff613b39f55d1378b (patch) | |
tree | 767ca2045743df3178b564e5f5930310aa161e94 | |
parent | d9f30bc50a7451ce38e75e6ec02a67f86b889df5 (diff) | |
download | libusb-d250964e64c97d92460c96fff613b39f55d1378b.tar.gz |
linux: Add a linux_netlink_read_message helper function
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
-rw-r--r-- | libusb/os/linux_netlink.c | 68 | ||||
-rw-r--r-- | libusb/version_nano.h | 2 |
2 files changed, 40 insertions, 30 deletions
diff --git a/libusb/os/linux_netlink.c b/libusb/os/linux_netlink.c index c7fbaeb..17eb944 100644 --- a/libusb/os/linux_netlink.c +++ b/libusb/os/linux_netlink.c @@ -127,10 +127,12 @@ static int linux_netlink_parse(char *buffer, size_t len, int *detached, const ch *devaddr = 0; tmp = netlink_message_parse((const char *) buffer, len, "ACTION"); + if (tmp == NULL) + return -1; if (0 == strcmp(tmp, "remove")) { *detached = 1; } else if (0 != strcmp(tmp, "add")) { - usbi_dbg("unknown device action"); + usbi_dbg("unknown device action %s", tmp); return -1; } @@ -180,51 +182,59 @@ static int linux_netlink_parse(char *buffer, size_t len, int *detached, const ch return 0; } -static void *linux_netlink_event_thread_main(void *arg) +static int linux_netlink_read_message(void) { - struct pollfd fds = {.fd = linux_netlink_socket, - .events = POLLIN}; unsigned char buffer[1024]; struct iovec iov = {.iov_base = buffer, .iov_len = sizeof(buffer)}; struct msghdr meh = { .msg_iov=&iov, .msg_iovlen=1, .msg_name=&snl, .msg_namelen=sizeof(snl) }; + const char *sys_name = NULL; uint8_t busnum, devaddr; int detached, r; size_t len; + /* read netlink message */ + memset(buffer, 0, sizeof(buffer)); + len = recvmsg(linux_netlink_socket, &meh, 0); + if (len < 32) { + if (errno != EAGAIN) + usbi_dbg("error recieving message from netlink"); + return -1; + } + + /* TODO -- authenticate this message is from the kernel or udevd */ + + r = linux_netlink_parse(buffer, len, &detached, &sys_name, + &busnum, &devaddr); + if (r) + return r; + + usbi_dbg("netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s", + busnum, devaddr, sys_name, detached ? "yes" : "no"); + + /* signal device is available (or not) to all contexts */ + if (detached) + linux_hotplug_disconnected(busnum, devaddr, sys_name); + else + linux_hotplug_enumerate(busnum, devaddr, sys_name); + + return 0; +} + +static void *linux_netlink_event_thread_main(void *arg) +{ + struct pollfd fds = {.fd = linux_netlink_socket, + .events = POLLIN}; + /* silence compiler warning */ (void) arg; while (1 == poll(&fds, 1, -1)) { - const char *sys_name = NULL; - if (POLLIN != fds.revents) { break; } - /* read netlink message */ - memset(buffer, 0, sizeof(buffer)); - len = recvmsg(linux_netlink_socket, &meh, 0); - if (len < 32) { - usbi_dbg("error recieving message from netlink"); - continue; - } - - /* TODO -- authenticate this message is from the kernel or udevd */ - - r = linux_netlink_parse(buffer, len, &detached, &sys_name, - &busnum, &devaddr); - if (r) - continue; - - usbi_dbg("netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s", - busnum, devaddr, sys_name, detached ? "yes" : "no"); - - /* signal device is available (or not) to all contexts */ - if (detached) - linux_hotplug_disconnected(busnum, devaddr, sys_name); - else - linux_hotplug_enumerate(busnum, devaddr, sys_name); + linux_netlink_read_message(); } return NULL; diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 27ee56f..297caf8 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 10721 +#define LIBUSB_NANO 10722 |