diff options
| -rw-r--r-- | src/client.c | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/client.c b/src/client.c index 8f7d945..65dc4ce 100644 --- a/src/client.c +++ b/src/client.c | |||
| @@ -31,6 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |||
| 31 | #include <sys/socket.h> | 31 | #include <sys/socket.h> |
| 32 | #include <sys/un.h> | 32 | #include <sys/un.h> |
| 33 | #include <arpa/inet.h> | 33 | #include <arpa/inet.h> |
| 34 | #include <pthread.h> | ||
| 34 | 35 | ||
| 35 | #ifdef HAVE_PLIST | 36 | #ifdef HAVE_PLIST |
| 36 | #include <plist/plist.h> | 37 | #include <plist/plist.h> |
| @@ -73,6 +74,7 @@ struct mux_client { | |||
| 73 | }; | 74 | }; |
| 74 | 75 | ||
| 75 | static struct collection client_list; | 76 | static struct collection client_list; |
| 77 | pthread_mutex_t client_list_mutex; | ||
| 76 | 78 | ||
| 77 | int client_read(struct mux_client *client, void *buffer, uint32_t len) | 79 | int client_read(struct mux_client *client, void *buffer, uint32_t len) |
| 78 | { | 80 | { |
| @@ -131,7 +133,9 @@ int client_accept(int listenfd) | |||
| 131 | client->state = CLIENT_COMMAND; | 133 | client->state = CLIENT_COMMAND; |
| 132 | client->events = POLLIN; | 134 | client->events = POLLIN; |
| 133 | 135 | ||
| 136 | pthread_mutex_lock(&client_list_mutex); | ||
| 134 | collection_add(&client_list, client); | 137 | collection_add(&client_list, client); |
| 138 | pthread_mutex_unlock(&client_list_mutex); | ||
| 135 | 139 | ||
| 136 | usbmuxd_log(LL_INFO, "New client on fd %d", client->fd); | 140 | usbmuxd_log(LL_INFO, "New client on fd %d", client->fd); |
| 137 | return client->fd; | 141 | return client->fd; |
| @@ -150,15 +154,19 @@ void client_close(struct mux_client *client) | |||
| 150 | free(client->ob_buf); | 154 | free(client->ob_buf); |
| 151 | if(client->ib_buf) | 155 | if(client->ib_buf) |
| 152 | free(client->ib_buf); | 156 | free(client->ib_buf); |
| 157 | pthread_mutex_lock(&client_list_mutex); | ||
| 153 | collection_remove(&client_list, client); | 158 | collection_remove(&client_list, client); |
| 159 | pthread_mutex_unlock(&client_list_mutex); | ||
| 154 | free(client); | 160 | free(client); |
| 155 | } | 161 | } |
| 156 | 162 | ||
| 157 | void client_get_fds(struct fdlist *list) | 163 | void client_get_fds(struct fdlist *list) |
| 158 | { | 164 | { |
| 165 | pthread_mutex_lock(&client_list_mutex); | ||
| 159 | FOREACH(struct mux_client *client, &client_list) { | 166 | FOREACH(struct mux_client *client, &client_list) { |
| 160 | fdlist_add(list, FD_CLIENT, client->fd, client->events); | 167 | fdlist_add(list, FD_CLIENT, client->fd, client->events); |
| 161 | } ENDFOREACH | 168 | } ENDFOREACH |
| 169 | pthread_mutex_unlock(&client_list_mutex); | ||
| 162 | } | 170 | } |
| 163 | 171 | ||
| 164 | static int send_pkt(struct mux_client *client, uint32_t tag, enum usbmuxd_msgtype msg, void *payload, int payload_length) | 172 | static int send_pkt(struct mux_client *client, uint32_t tag, enum usbmuxd_msgtype msg, void *payload, int payload_length) |
| @@ -556,12 +564,14 @@ static void process_recv(struct mux_client *client) | |||
| 556 | void client_process(int fd, short events) | 564 | void client_process(int fd, short events) |
| 557 | { | 565 | { |
| 558 | struct mux_client *client = NULL; | 566 | struct mux_client *client = NULL; |
| 567 | pthread_mutex_lock(&client_list_mutex); | ||
| 559 | FOREACH(struct mux_client *lc, &client_list) { | 568 | FOREACH(struct mux_client *lc, &client_list) { |
| 560 | if(lc->fd == fd) { | 569 | if(lc->fd == fd) { |
| 561 | client = lc; | 570 | client = lc; |
| 562 | break; | 571 | break; |
| 563 | } | 572 | } |
| 564 | } ENDFOREACH | 573 | } ENDFOREACH |
| 574 | pthread_mutex_unlock(&client_list_mutex); | ||
| 565 | 575 | ||
| 566 | if(!client) { | 576 | if(!client) { |
| 567 | usbmuxd_log(LL_INFO, "client_process: fd %d not found in client list", fd); | 577 | usbmuxd_log(LL_INFO, "client_process: fd %d not found in client list", fd); |
| @@ -583,28 +593,33 @@ void client_process(int fd, short events) | |||
| 583 | 593 | ||
| 584 | void client_device_add(struct device_info *dev) | 594 | void client_device_add(struct device_info *dev) |
| 585 | { | 595 | { |
| 596 | pthread_mutex_lock(&client_list_mutex); | ||
| 586 | usbmuxd_log(LL_DEBUG, "client_device_add: id %d, location 0x%x, serial %s", dev->id, dev->location, dev->serial); | 597 | usbmuxd_log(LL_DEBUG, "client_device_add: id %d, location 0x%x, serial %s", dev->id, dev->location, dev->serial); |
| 587 | device_set_visible(dev->id); | 598 | device_set_visible(dev->id); |
| 588 | FOREACH(struct mux_client *client, &client_list) { | 599 | FOREACH(struct mux_client *client, &client_list) { |
| 589 | if(client->state == CLIENT_LISTEN) | 600 | if(client->state == CLIENT_LISTEN) |
| 590 | notify_device_add(client, dev); | 601 | notify_device_add(client, dev); |
| 591 | } ENDFOREACH | 602 | } ENDFOREACH |
| 603 | pthread_mutex_unlock(&client_list_mutex); | ||
| 592 | } | 604 | } |
| 605 | |||
| 593 | void client_device_remove(int device_id) | 606 | void client_device_remove(int device_id) |
| 594 | { | 607 | { |
| 608 | pthread_mutex_lock(&client_list_mutex); | ||
| 595 | uint32_t id = device_id; | 609 | uint32_t id = device_id; |
| 596 | usbmuxd_log(LL_DEBUG, "client_device_remove: id %d", device_id); | 610 | usbmuxd_log(LL_DEBUG, "client_device_remove: id %d", device_id); |
| 597 | FOREACH(struct mux_client *client, &client_list) { | 611 | FOREACH(struct mux_client *client, &client_list) { |
| 598 | if(client->state == CLIENT_LISTEN) | 612 | if(client->state == CLIENT_LISTEN) |
| 599 | notify_device_remove(client, id); | 613 | notify_device_remove(client, id); |
| 600 | } ENDFOREACH | 614 | } ENDFOREACH |
| 615 | pthread_mutex_unlock(&client_list_mutex); | ||
| 601 | } | 616 | } |
| 602 | 617 | ||
| 603 | |||
| 604 | void client_init(void) | 618 | void client_init(void) |
| 605 | { | 619 | { |
| 606 | usbmuxd_log(LL_DEBUG, "client_init"); | 620 | usbmuxd_log(LL_DEBUG, "client_init"); |
| 607 | collection_init(&client_list); | 621 | collection_init(&client_list); |
| 622 | pthread_mutex_init(&client_list_mutex, NULL); | ||
| 608 | } | 623 | } |
| 609 | 624 | ||
| 610 | void client_shutdown(void) | 625 | void client_shutdown(void) |
| @@ -613,5 +628,6 @@ void client_shutdown(void) | |||
| 613 | FOREACH(struct mux_client *client, &client_list) { | 628 | FOREACH(struct mux_client *client, &client_list) { |
| 614 | client_close(client); | 629 | client_close(client); |
| 615 | } ENDFOREACH | 630 | } ENDFOREACH |
| 631 | pthread_mutex_destroy(&client_list_mutex); | ||
| 616 | collection_free(&client_list); | 632 | collection_free(&client_list); |
| 617 | } | 633 | } |
