diff options
| author | 2010-11-26 03:09:01 +0100 | |
|---|---|---|
| committer | 2010-11-26 03:09:01 +0100 | |
| commit | 31cd1980051bfffeda2ef7cac2986548a2e98107 (patch) | |
| tree | 8b29af26c3745ebd46003203cf78bf950982e12c /libusbmuxd/libusbmuxd.c | |
| parent | e534cc5ce4c7fa42c9314b26d7fa0cf288d39833 (diff) | |
| download | usbmuxd-31cd1980051bfffeda2ef7cac2986548a2e98107.tar.gz usbmuxd-31cd1980051bfffeda2ef7cac2986548a2e98107.tar.bz2 | |
libusbmuxd: implement inotify support
Diffstat (limited to 'libusbmuxd/libusbmuxd.c')
| -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 | |||
| 34 | #include <sys/socket.h> | 34 | #include <sys/socket.h> |
| 35 | #include <arpa/inet.h> | 35 | #include <arpa/inet.h> |
| 36 | #endif | 36 | #endif |
| 37 | |||
| 38 | #ifdef HAVE_INOTIFY | ||
| 39 | #include <sys/inotify.h> | ||
| 40 | #define EVENT_SIZE (sizeof (struct inotify_event)) | ||
| 41 | #define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16)) | ||
| 42 | #define USBMUXD_DIRNAME "/var/run" | ||
| 43 | #define USBMUXD_SOCKET_NAME "usbmuxd" | ||
| 44 | #endif /* HAVE_INOTIFY */ | ||
| 45 | |||
| 37 | #include <unistd.h> | 46 | #include <unistd.h> |
| 38 | #include <signal.h> | 47 | #include <signal.h> |
| 39 | #include <pthread.h> | 48 | #include <pthread.h> |
| @@ -374,19 +383,10 @@ static void generate_event(usbmuxd_event_cb_t callback, const usbmuxd_device_inf | |||
| 374 | callback(&ev, user_data); | 383 | callback(&ev, user_data); |
| 375 | } | 384 | } |
| 376 | 385 | ||
| 377 | /** | 386 | static int usbmuxd_listen_poll() |
| 378 | * Tries to connect to usbmuxd and wait if it is not running. | ||
| 379 | * | ||
| 380 | * TODO inotify support should come here | ||
| 381 | */ | ||
| 382 | static int usbmuxd_listen() | ||
| 383 | { | 387 | { |
| 384 | int sfd; | 388 | int sfd; |
| 385 | uint32_t res = -1; | ||
| 386 | 389 | ||
| 387 | #ifdef HAVE_PLIST | ||
| 388 | retry: | ||
| 389 | #endif | ||
| 390 | sfd = connect_usbmuxd_socket(); | 390 | sfd = connect_usbmuxd_socket(); |
| 391 | if (sfd < 0) { | 391 | if (sfd < 0) { |
| 392 | while (event_cb) { | 392 | while (event_cb) { |
| @@ -397,6 +397,82 @@ retry: | |||
| 397 | } | 397 | } |
| 398 | } | 398 | } |
| 399 | 399 | ||
| 400 | return sfd; | ||
| 401 | } | ||
| 402 | |||
| 403 | #ifdef HAVE_INOTIFY | ||
| 404 | static int usbmuxd_listen_inotify() | ||
| 405 | { | ||
| 406 | int inot_fd; | ||
| 407 | int watch_fd; | ||
| 408 | int sfd;; | ||
| 409 | |||
| 410 | sfd = -1; | ||
| 411 | inot_fd = inotify_init (); | ||
| 412 | if (inot_fd < 0) { | ||
| 413 | fprintf (stderr, "Failed to setup inotify\n"); | ||
| 414 | return -1; | ||
| 415 | } | ||
| 416 | |||
| 417 | /* inotify is setup, listen for events that concern us */ | ||
| 418 | watch_fd = inotify_add_watch (inot_fd, USBMUXD_DIRNAME, IN_CREATE); | ||
| 419 | if (watch_fd < 0) { | ||
| 420 | fprintf (stderr, "Failed to setup watch for socket dir\n"); | ||
| 421 | close (inot_fd); | ||
| 422 | return -1; | ||
| 423 | } | ||
| 424 | |||
| 425 | while (1) { | ||
| 426 | ssize_t len, i; | ||
| 427 | char buff[EVENT_BUF_LEN] = {0}; | ||
| 428 | |||
| 429 | i = 0; | ||
| 430 | len = read (inot_fd, buff, EVENT_BUF_LEN -1); | ||
| 431 | if (len < 0) | ||
| 432 | goto end; | ||
| 433 | while (i < len) { | ||
| 434 | struct inotify_event *pevent = (struct inotify_event *) & buff[i]; | ||
| 435 | |||
| 436 | /* check that it's ours */ | ||
| 437 | if (pevent->mask & IN_CREATE && | ||
| 438 | pevent->len && | ||
| 439 | pevent->name != NULL && | ||
| 440 | strcmp(pevent->name, USBMUXD_SOCKET_NAME) == 0) { | ||
| 441 | sfd = connect_usbmuxd_socket (); | ||
| 442 | goto end; | ||
| 443 | } | ||
| 444 | i += EVENT_SIZE + pevent->len; | ||
| 445 | } | ||
| 446 | } | ||
| 447 | |||
| 448 | end: | ||
| 449 | close(watch_fd); | ||
| 450 | close(inot_fd); | ||
| 451 | |||
| 452 | return sfd; | ||
| 453 | } | ||
| 454 | #endif /* HAVE_INOTIFY */ | ||
| 455 | |||
| 456 | /** | ||
| 457 | * Tries to connect to usbmuxd and wait if it is not running. | ||
| 458 | */ | ||
| 459 | static int usbmuxd_listen() | ||
| 460 | { | ||
| 461 | int sfd; | ||
| 462 | uint32_t res = -1; | ||
| 463 | |||
| 464 | #ifdef HAVE_PLIST | ||
| 465 | retry: | ||
| 466 | #endif | ||
| 467 | |||
| 468 | #ifdef HAVE_INOTIFY | ||
| 469 | sfd = usbmuxd_listen_inotify(); | ||
| 470 | if (sfd < 0) | ||
| 471 | sfd = usbmuxd_listen_poll(); | ||
| 472 | #else | ||
| 473 | sfd = usbmuxd_listen_poll(); | ||
| 474 | #endif | ||
| 475 | |||
| 400 | if (sfd < 0) { | 476 | if (sfd < 0) { |
| 401 | fprintf(stderr, "%s: ERROR: usbmuxd was supposed to be running here...\n", __func__); | 477 | fprintf(stderr, "%s: ERROR: usbmuxd was supposed to be running here...\n", __func__); |
| 402 | return sfd; | 478 | return sfd; |
