Misc: Trim and consolidate header file usage
[platform/upstream/libusb.git] / libusb / os / linux_udev.c
index 61d953d..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 "libusbi.h"
-#include "linux_usbfs.h"
 
 /* udev context */
 static struct udev *udev_ctx = NULL;
@@ -82,17 +71,33 @@ int linux_udev_start_event_monitor(void)
 
        udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
 
+#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, "getting udev monitor fd flags (%d)", errno);
+               usbi_err(NULL, "failed to get udev monitor fd status flags, errno=%d", errno);
                goto err_free_monitor;
        }
-       r = fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK);
-       if (r) {
-               usbi_err(NULL, "setting udev monitor fd flags (%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);
@@ -134,7 +139,7 @@ int linux_udev_stop_event_monitor(void)
 
        /* Write some dummy data to the control pipe and
         * wait for the thread to exit */
-       r = usbi_write(udev_control_pipe[1], &dummy, sizeof(dummy));
+       r = write(udev_control_pipe[1], &dummy, sizeof(dummy));
        if (r <= 0) {
                usbi_warn(NULL, "udev control pipe signal failed");
        }
@@ -162,6 +167,7 @@ static void *linux_udev_event_thread_main(void *arg)
 {
        char dummy;
        int r;
+       ssize_t nb;
        struct udev_device* udev_dev;
        struct pollfd fds[] = {
                {.fd = udev_control_pipe[0],
@@ -179,8 +185,8 @@ static void *linux_udev_event_thread_main(void *arg)
                }
                if (fds[0].revents & POLLIN) {
                        /* activity on control pipe, read the byte and exit */
-                       r = usbi_read(udev_control_pipe[0], &dummy, sizeof(dummy));
-                       if (r <= 0) {
+                       nb = read(udev_control_pipe[0], &dummy, sizeof(dummy));
+                       if (nb <= 0) {
                                usbi_warn(NULL, "udev control pipe read failed");
                        }
                        break;
@@ -215,7 +221,7 @@ 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(struct udev_device* udev_dev)
@@ -245,6 +251,8 @@ static void udev_hotplug_event(struct udev_device* udev_dev)
                        linux_hotplug_enumerate(busnum, devaddr, sys_name);
                } else if (detached) {
                        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);
                }
@@ -274,6 +282,7 @@ int linux_udev_scan_devices(struct libusb_context *ctx)
        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;