diff options
Diffstat (limited to 'libusbmuxd')
-rw-r--r-- | libusbmuxd/libusbmuxd.c | 96 |
1 files changed, 86 insertions, 10 deletions
diff --git a/libusbmuxd/libusbmuxd.c b/libusbmuxd/libusbmuxd.c index 5eaf8e6..5735a72 100644 --- a/libusbmuxd/libusbmuxd.c +++ b/libusbmuxd/libusbmuxd.c @@ -34,6 +34,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include <sys/socket.h> #include <arpa/inet.h> #endif + +#ifdef HAVE_INOTIFY +#include <sys/inotify.h> +#define EVENT_SIZE (sizeof (struct inotify_event)) +#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16)) +#define USBMUXD_DIRNAME "/var/run" +#define USBMUXD_SOCKET_NAME "usbmuxd" +#endif /* HAVE_INOTIFY */ + #include <unistd.h> #include <signal.h> #include <pthread.h> @@ -374,19 +383,10 @@ static void generate_event(usbmuxd_event_cb_t callback, const usbmuxd_device_inf callback(&ev, user_data); } -/** - * Tries to connect to usbmuxd and wait if it is not running. - * - * TODO inotify support should come here - */ -static int usbmuxd_listen() +static int usbmuxd_listen_poll() { int sfd; - uint32_t res = -1; -#ifdef HAVE_PLIST -retry: -#endif sfd = connect_usbmuxd_socket(); if (sfd < 0) { while (event_cb) { @@ -397,6 +397,82 @@ retry: } } + return sfd; +} + +#ifdef HAVE_INOTIFY +static int usbmuxd_listen_inotify() +{ + int inot_fd; + int watch_fd; + int sfd;; + + sfd = -1; + inot_fd = inotify_init (); + if (inot_fd < 0) { + fprintf (stderr, "Failed to setup inotify\n"); + return -1; + } + + /* inotify is setup, listen for events that concern us */ + watch_fd = inotify_add_watch (inot_fd, USBMUXD_DIRNAME, IN_CREATE); + if (watch_fd < 0) { + fprintf (stderr, "Failed to setup watch for socket dir\n"); + close (inot_fd); + return -1; + } + + while (1) { + ssize_t len, i; + char buff[EVENT_BUF_LEN] = {0}; + + i = 0; + len = read (inot_fd, buff, EVENT_BUF_LEN -1); + if (len < 0) + goto end; + while (i < len) { + struct inotify_event *pevent = (struct inotify_event *) & buff[i]; + + /* check that it's ours */ + if (pevent->mask & IN_CREATE && + pevent->len && + pevent->name != NULL && + strcmp(pevent->name, USBMUXD_SOCKET_NAME) == 0) { + sfd = connect_usbmuxd_socket (); + goto end; + } + i += EVENT_SIZE + pevent->len; + } + } + +end: + close(watch_fd); + close(inot_fd); + + return sfd; +} +#endif /* HAVE_INOTIFY */ + +/** + * Tries to connect to usbmuxd and wait if it is not running. + */ +static int usbmuxd_listen() +{ + int sfd; + uint32_t res = -1; + +#ifdef HAVE_PLIST +retry: +#endif + +#ifdef HAVE_INOTIFY + sfd = usbmuxd_listen_inotify(); + if (sfd < 0) + sfd = usbmuxd_listen_poll(); +#else + sfd = usbmuxd_listen_poll(); +#endif + if (sfd < 0) { fprintf(stderr, "%s: ERROR: usbmuxd was supposed to be running here...\n", __func__); return sfd; |