Misc: Trim and consolidate header file usage
[platform/upstream/libusb.git] / libusb / os / linux_udev.c
index 9045c2e..d079c79 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "config.h"
+#include "libusbi.h"
+#include "linux_usbfs.h"
 
-#include <assert.h>
-#include <ctype.h>
-#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <libudev.h>
 #include <poll.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <pthread.h>
 #include <string.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/utsname.h>
-#include <sys/socket.h>
 #include <unistd.h>
-#include <libudev.h>
-
-#include "libusb.h"
-#include "libusbi.h"
-#include "linux_usbfs.h"
 
 /* udev context */
 static struct udev *udev_ctx = NULL;
 static int udev_monitor_fd = -1;
+static int udev_control_pipe[2] = {-1, -1};
 static struct udev_monitor *udev_monitor = NULL;
 static pthread_t linux_event_thread;
 
-static void udev_hotplug_event(void);
+static void udev_hotplug_event(struct udev_device* udev_dev);
 static void *linux_udev_event_thread_main(void *arg);
 
 int linux_udev_start_event_monitor(void)
@@ -60,7 +49,7 @@ int linux_udev_start_event_monitor(void)
        udev_ctx = udev_new();
        if (!udev_ctx) {
                usbi_err(NULL, "could not create udev context");
-               return LIBUSB_ERROR_OTHER;
+               goto err;
        }
 
        udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
@@ -69,7 +58,7 @@ int linux_udev_start_event_monitor(void)
                goto err_free_ctx;
        }
 
-       r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0);
+       r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device");
        if (r) {
                usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
                goto err_free_monitor;
@@ -82,34 +71,79 @@ int linux_udev_start_event_monitor(void)
 
        udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
 
-       r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
+#if defined(FD_CLOEXEC)
+       /* Make sure the udev file descriptor is marked as CLOEXEC */
+       r = fcntl(udev_monitor_fd, F_GETFD);
+       if (r == -1) {
+               usbi_err(NULL, "failed to get udev monitor fd flags, errno=%d", errno);
+               goto err_free_monitor;
+       }
+       if (!(r & FD_CLOEXEC)) {
+               if (fcntl(udev_monitor_fd, F_SETFD, r | FD_CLOEXEC) == -1) {
+                       usbi_err(NULL, "failed to set udev monitor fd flags, errno=%d", errno);
+                       goto err_free_monitor;
+               }
+       }
+#endif
+
+       /* Some older versions of udev are not non-blocking by default,
+        * so make sure this is set */
+       r = fcntl(udev_monitor_fd, F_GETFL);
+       if (r == -1) {
+               usbi_err(NULL, "failed to get udev monitor fd status flags, errno=%d", errno);
+               goto err_free_monitor;
+       }
+       if (!(r & O_NONBLOCK)) {
+               if (fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK) == -1) {
+                       usbi_err(NULL, "failed to set udev monitor fd status flags, errno=%d", errno);
+                       goto err_free_monitor;
+               }
+       }
+
+       r = usbi_pipe(udev_control_pipe);
        if (r) {
-               usbi_err(NULL, "could not create linux hotplug event thread");
+               usbi_err(NULL, "could not create udev control pipe");
                goto err_free_monitor;
        }
 
+       r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
+       if (r) {
+               usbi_err(NULL, "creating hotplug event thread (%d)", r);
+               goto err_close_pipe;
+       }
+
        return LIBUSB_SUCCESS;
 
+err_close_pipe:
+       close(udev_control_pipe[0]);
+       close(udev_control_pipe[1]);
 err_free_monitor:
        udev_monitor_unref(udev_monitor);
        udev_monitor = NULL;
        udev_monitor_fd = -1;
 err_free_ctx:
        udev_unref(udev_ctx);
+err:
        udev_ctx = NULL;
        return LIBUSB_ERROR_OTHER;
 }
 
 int linux_udev_stop_event_monitor(void)
 {
+       char dummy = 1;
+       int r;
+
        assert(udev_ctx != NULL);
        assert(udev_monitor != NULL);
        assert(udev_monitor_fd != -1);
 
-       /* Cancel the event thread. This is the only way to garauntee the thread
-          exits since closing the monitor fd won't necessarily cause poll
-          to return. */
-       pthread_cancel(linux_event_thread);
+       /* Write some dummy data to the control pipe and
+        * wait for the thread to exit */
+       r = write(udev_control_pipe[1], &dummy, sizeof(dummy));
+       if (r <= 0) {
+               usbi_warn(NULL, "udev control pipe signal failed");
+       }
+       pthread_join(linux_event_thread, NULL);
 
        /* Release the udev monitor */
        udev_monitor_unref(udev_monitor);
@@ -120,22 +154,50 @@ int linux_udev_stop_event_monitor(void)
        udev_unref(udev_ctx);
        udev_ctx = NULL;
 
+       /* close and reset control pipe */
+       close(udev_control_pipe[0]);
+       close(udev_control_pipe[1]);
+       udev_control_pipe[0] = -1;
+       udev_control_pipe[1] = -1;
+
        return LIBUSB_SUCCESS;
 }
 
 static void *linux_udev_event_thread_main(void *arg)
 {
-       struct pollfd fds = {.fd = udev_monitor_fd,
-                            .events = POLLIN};
+       char dummy;
+       int r;
+       ssize_t nb;
+       struct udev_device* udev_dev;
+       struct pollfd fds[] = {
+               {.fd = udev_control_pipe[0],
+                .events = POLLIN},
+               {.fd = udev_monitor_fd,
+                .events = POLLIN},
+       };
 
        usbi_dbg("udev event thread entering.");
 
-       while (1 == poll(&fds, 1, -1)) {
-               if (NULL == udev_monitor || POLLIN != fds.revents) {
+       while ((r = poll(fds, 2, -1)) >= 0 || errno == EINTR) {
+               if (r < 0) {
+                       /* temporary failure */
+                       continue;
+               }
+               if (fds[0].revents & POLLIN) {
+                       /* activity on control pipe, read the byte and exit */
+                       nb = read(udev_control_pipe[0], &dummy, sizeof(dummy));
+                       if (nb <= 0) {
+                               usbi_warn(NULL, "udev control pipe read failed");
+                       }
                        break;
                }
-
-               udev_hotplug_event();
+               if (fds[1].revents & POLLIN) {
+                       usbi_mutex_static_lock(&linux_hotplug_lock);
+                       udev_dev = udev_monitor_receive_device(udev_monitor);
+                       if (udev_dev)
+                               udev_hotplug_event(udev_dev);
+                       usbi_mutex_static_unlock(&linux_hotplug_lock);
+               }
        }
 
        usbi_dbg("udev event thread exiting");
@@ -159,29 +221,18 @@ static int udev_device_info(struct libusb_context *ctx, int detached,
        }
 
        return linux_get_device_address(ctx, detached, busnum, devaddr,
-                                       dev_node, *sys_name);
+                                       dev_node, *sys_name, -1);
 }
 
-static void udev_hotplug_event(void)
+static void udev_hotplug_event(struct udev_device* udev_dev)
 {
-       struct udev_device* udev_dev;
        const char* udev_action;
        const char* sys_name = NULL;
        uint8_t busnum = 0, devaddr = 0;
        int detached;
        int r;
 
-       if (NULL == udev_monitor) {
-               return;
-       }
-
        do {
-               udev_dev = udev_monitor_receive_device(udev_monitor);
-               if (!udev_dev) {
-                       usbi_err(NULL, "failed to read data from udev monitor socket.");
-                       return;
-               }
-
                udev_action = udev_device_get_action(udev_dev);
                if (!udev_action) {
                        break;
@@ -199,7 +250,9 @@ static void udev_hotplug_event(void)
                if (strncmp(udev_action, "add", 3) == 0) {
                        linux_hotplug_enumerate(busnum, devaddr, sys_name);
                } else if (detached) {
-                       linux_hotplug_disconnected(busnum, devaddr, sys_name);
+                       linux_device_disconnected(busnum, devaddr);
+               } else if (strncmp(udev_action, "bind", 4) == 0) {
+                       /* silently ignore "known unhandled" action */
                } else {
                        usbi_err(NULL, "ignoring udev action %s", udev_action);
                }
@@ -225,9 +278,11 @@ int linux_udev_scan_devices(struct libusb_context *ctx)
        }
 
        udev_enumerate_add_match_subsystem(enumerator, "usb");
+       udev_enumerate_add_match_property(enumerator, "DEVTYPE", "usb_device");
        udev_enumerate_scan_devices(enumerator);
        devices = udev_enumerate_get_list_entry(enumerator);
 
+       entry = NULL;
        udev_list_entry_foreach(entry, devices) {
                const char *path = udev_list_entry_get_name(entry);
                uint8_t busnum = 0, devaddr = 0;
@@ -248,3 +303,18 @@ int linux_udev_scan_devices(struct libusb_context *ctx)
 
        return LIBUSB_SUCCESS;
 }
+
+void linux_udev_hotplug_poll(void)
+{
+       struct udev_device* udev_dev;
+
+       usbi_mutex_static_lock(&linux_hotplug_lock);
+       do {
+               udev_dev = udev_monitor_receive_device(udev_monitor);
+               if (udev_dev) {
+                       usbi_dbg("Handling hotplug event from hotplug_poll");
+                       udev_hotplug_event(udev_dev);
+               }
+       } while (udev_dev);
+       usbi_mutex_static_unlock(&linux_hotplug_lock);
+}