diff options
Diffstat (limited to 'src/client.c')
-rw-r--r-- | src/client.c | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/src/client.c b/src/client.c index 7395046..75a526d 100644 --- a/src/client.c +++ b/src/client.c | |||
@@ -34,10 +34,11 @@ | |||
34 | #include <netinet/tcp.h> | 34 | #include <netinet/tcp.h> |
35 | #include <sys/un.h> | 35 | #include <sys/un.h> |
36 | #include <arpa/inet.h> | 36 | #include <arpa/inet.h> |
37 | #include <pthread.h> | ||
38 | #include <fcntl.h> | 37 | #include <fcntl.h> |
39 | 38 | ||
40 | #include <plist/plist.h> | 39 | #include <plist/plist.h> |
40 | #include <libimobiledevice-glue/collection.h> | ||
41 | #include <libimobiledevice-glue/thread.h> | ||
41 | 42 | ||
42 | #include "log.h" | 43 | #include "log.h" |
43 | #include "usb.h" | 44 | #include "usb.h" |
@@ -75,7 +76,7 @@ struct mux_client { | |||
75 | }; | 76 | }; |
76 | 77 | ||
77 | static struct collection client_list; | 78 | static struct collection client_list; |
78 | pthread_mutex_t client_list_mutex; | 79 | mutex_t client_list_mutex; |
79 | static uint32_t client_number = 0; | 80 | static uint32_t client_number = 0; |
80 | 81 | ||
81 | #ifdef SO_PEERCRED | 82 | #ifdef SO_PEERCRED |
@@ -224,10 +225,10 @@ int client_accept(int listenfd) | |||
224 | client->events = POLLIN; | 225 | client->events = POLLIN; |
225 | client->info = NULL; | 226 | client->info = NULL; |
226 | 227 | ||
227 | pthread_mutex_lock(&client_list_mutex); | 228 | mutex_lock(&client_list_mutex); |
228 | client->number = client_number++; | 229 | client->number = client_number++; |
229 | collection_add(&client_list, client); | 230 | collection_add(&client_list, client); |
230 | pthread_mutex_unlock(&client_list_mutex); | 231 | mutex_unlock(&client_list_mutex); |
231 | 232 | ||
232 | #ifdef SO_PEERCRED | 233 | #ifdef SO_PEERCRED |
233 | if (log_level >= LL_INFO) { | 234 | if (log_level >= LL_INFO) { |
@@ -252,7 +253,7 @@ int client_accept(int listenfd) | |||
252 | void client_close(struct mux_client *client) | 253 | void client_close(struct mux_client *client) |
253 | { | 254 | { |
254 | int found = 0; | 255 | int found = 0; |
255 | pthread_mutex_lock(&client_list_mutex); | 256 | mutex_lock(&client_list_mutex); |
256 | FOREACH(struct mux_client *lc, &client_list) { | 257 | FOREACH(struct mux_client *lc, &client_list) { |
257 | if (client == lc) { | 258 | if (client == lc) { |
258 | found = 1; | 259 | found = 1; |
@@ -262,7 +263,7 @@ void client_close(struct mux_client *client) | |||
262 | if (!found) { | 263 | if (!found) { |
263 | // in case we get called again but client was already freed | 264 | // in case we get called again but client was already freed |
264 | usbmuxd_log(LL_DEBUG, "%s: ignoring for non-existing client %p", __func__, client); | 265 | usbmuxd_log(LL_DEBUG, "%s: ignoring for non-existing client %p", __func__, client); |
265 | pthread_mutex_unlock(&client_list_mutex); | 266 | mutex_unlock(&client_list_mutex); |
266 | return; | 267 | return; |
267 | } | 268 | } |
268 | #ifdef SO_PEERCRED | 269 | #ifdef SO_PEERCRED |
@@ -293,17 +294,17 @@ void client_close(struct mux_client *client) | |||
293 | plist_free(client->info); | 294 | plist_free(client->info); |
294 | 295 | ||
295 | collection_remove(&client_list, client); | 296 | collection_remove(&client_list, client); |
296 | pthread_mutex_unlock(&client_list_mutex); | 297 | mutex_unlock(&client_list_mutex); |
297 | free(client); | 298 | free(client); |
298 | } | 299 | } |
299 | 300 | ||
300 | void client_get_fds(struct fdlist *list) | 301 | void client_get_fds(struct fdlist *list) |
301 | { | 302 | { |
302 | pthread_mutex_lock(&client_list_mutex); | 303 | mutex_lock(&client_list_mutex); |
303 | FOREACH(struct mux_client *client, &client_list) { | 304 | FOREACH(struct mux_client *client, &client_list) { |
304 | fdlist_add(list, FD_CLIENT, client->fd, client->events); | 305 | fdlist_add(list, FD_CLIENT, client->fd, client->events); |
305 | } ENDFOREACH | 306 | } ENDFOREACH |
306 | pthread_mutex_unlock(&client_list_mutex); | 307 | mutex_unlock(&client_list_mutex); |
307 | } | 308 | } |
308 | 309 | ||
309 | static int output_buffer_add_message(struct mux_client *client, uint32_t tag, enum usbmuxd_msgtype msg, void *payload, int payload_length) | 310 | static int output_buffer_add_message(struct mux_client *client, uint32_t tag, enum usbmuxd_msgtype msg, void *payload, int payload_length) |
@@ -442,7 +443,7 @@ static int send_listener_list(struct mux_client *client, uint32_t tag) | |||
442 | plist_t dict = plist_new_dict(); | 443 | plist_t dict = plist_new_dict(); |
443 | plist_t listeners = plist_new_array(); | 444 | plist_t listeners = plist_new_array(); |
444 | 445 | ||
445 | pthread_mutex_lock(&client_list_mutex); | 446 | mutex_lock(&client_list_mutex); |
446 | FOREACH(struct mux_client *lc, &client_list) { | 447 | FOREACH(struct mux_client *lc, &client_list) { |
447 | if (lc->state == CLIENT_LISTEN) { | 448 | if (lc->state == CLIENT_LISTEN) { |
448 | plist_t n = NULL; | 449 | plist_t n = NULL; |
@@ -489,7 +490,7 @@ static int send_listener_list(struct mux_client *client, uint32_t tag) | |||
489 | plist_array_append_item(listeners, l); | 490 | plist_array_append_item(listeners, l); |
490 | } | 491 | } |
491 | } ENDFOREACH | 492 | } ENDFOREACH |
492 | pthread_mutex_unlock(&client_list_mutex); | 493 | mutex_unlock(&client_list_mutex); |
493 | 494 | ||
494 | plist_dict_set_item(dict, "ListenerList", listeners); | 495 | plist_dict_set_item(dict, "ListenerList", listeners); |
495 | res = send_plist(client, tag, dict); | 496 | res = send_plist(client, tag, dict); |
@@ -975,14 +976,14 @@ static void input_buffer_process(struct mux_client *client) | |||
975 | void client_process(int fd, short events) | 976 | void client_process(int fd, short events) |
976 | { | 977 | { |
977 | struct mux_client *client = NULL; | 978 | struct mux_client *client = NULL; |
978 | pthread_mutex_lock(&client_list_mutex); | 979 | mutex_lock(&client_list_mutex); |
979 | FOREACH(struct mux_client *lc, &client_list) { | 980 | FOREACH(struct mux_client *lc, &client_list) { |
980 | if(lc->fd == fd) { | 981 | if(lc->fd == fd) { |
981 | client = lc; | 982 | client = lc; |
982 | break; | 983 | break; |
983 | } | 984 | } |
984 | } ENDFOREACH | 985 | } ENDFOREACH |
985 | pthread_mutex_unlock(&client_list_mutex); | 986 | mutex_unlock(&client_list_mutex); |
986 | 987 | ||
987 | if(!client) { | 988 | if(!client) { |
988 | usbmuxd_log(LL_INFO, "client_process: fd %d not found in client list", fd); | 989 | usbmuxd_log(LL_INFO, "client_process: fd %d not found in client list", fd); |
@@ -1004,45 +1005,45 @@ void client_process(int fd, short events) | |||
1004 | 1005 | ||
1005 | void client_device_add(struct device_info *dev) | 1006 | void client_device_add(struct device_info *dev) |
1006 | { | 1007 | { |
1007 | pthread_mutex_lock(&client_list_mutex); | 1008 | mutex_lock(&client_list_mutex); |
1008 | usbmuxd_log(LL_DEBUG, "client_device_add: id %d, location 0x%x, serial %s", dev->id, dev->location, dev->serial); | 1009 | usbmuxd_log(LL_DEBUG, "client_device_add: id %d, location 0x%x, serial %s", dev->id, dev->location, dev->serial); |
1009 | device_set_visible(dev->id); | 1010 | device_set_visible(dev->id); |
1010 | FOREACH(struct mux_client *client, &client_list) { | 1011 | FOREACH(struct mux_client *client, &client_list) { |
1011 | if(client->state == CLIENT_LISTEN) | 1012 | if(client->state == CLIENT_LISTEN) |
1012 | send_device_add(client, dev); | 1013 | send_device_add(client, dev); |
1013 | } ENDFOREACH | 1014 | } ENDFOREACH |
1014 | pthread_mutex_unlock(&client_list_mutex); | 1015 | mutex_unlock(&client_list_mutex); |
1015 | } | 1016 | } |
1016 | 1017 | ||
1017 | void client_device_remove(int device_id) | 1018 | void client_device_remove(int device_id) |
1018 | { | 1019 | { |
1019 | pthread_mutex_lock(&client_list_mutex); | 1020 | mutex_lock(&client_list_mutex); |
1020 | uint32_t id = device_id; | 1021 | uint32_t id = device_id; |
1021 | usbmuxd_log(LL_DEBUG, "client_device_remove: id %d", device_id); | 1022 | usbmuxd_log(LL_DEBUG, "client_device_remove: id %d", device_id); |
1022 | FOREACH(struct mux_client *client, &client_list) { | 1023 | FOREACH(struct mux_client *client, &client_list) { |
1023 | if(client->state == CLIENT_LISTEN) | 1024 | if(client->state == CLIENT_LISTEN) |
1024 | send_device_remove(client, id); | 1025 | send_device_remove(client, id); |
1025 | } ENDFOREACH | 1026 | } ENDFOREACH |
1026 | pthread_mutex_unlock(&client_list_mutex); | 1027 | mutex_unlock(&client_list_mutex); |
1027 | } | 1028 | } |
1028 | 1029 | ||
1029 | void client_device_paired(int device_id) | 1030 | void client_device_paired(int device_id) |
1030 | { | 1031 | { |
1031 | pthread_mutex_lock(&client_list_mutex); | 1032 | mutex_lock(&client_list_mutex); |
1032 | uint32_t id = device_id; | 1033 | uint32_t id = device_id; |
1033 | usbmuxd_log(LL_DEBUG, "client_device_paired: id %d", device_id); | 1034 | usbmuxd_log(LL_DEBUG, "client_device_paired: id %d", device_id); |
1034 | FOREACH(struct mux_client *client, &client_list) { | 1035 | FOREACH(struct mux_client *client, &client_list) { |
1035 | if (client->state == CLIENT_LISTEN) | 1036 | if (client->state == CLIENT_LISTEN) |
1036 | send_device_paired(client, id); | 1037 | send_device_paired(client, id); |
1037 | } ENDFOREACH | 1038 | } ENDFOREACH |
1038 | pthread_mutex_unlock(&client_list_mutex); | 1039 | mutex_unlock(&client_list_mutex); |
1039 | } | 1040 | } |
1040 | 1041 | ||
1041 | void client_init(void) | 1042 | void client_init(void) |
1042 | { | 1043 | { |
1043 | usbmuxd_log(LL_DEBUG, "client_init"); | 1044 | usbmuxd_log(LL_DEBUG, "client_init"); |
1044 | collection_init(&client_list); | 1045 | collection_init(&client_list); |
1045 | pthread_mutex_init(&client_list_mutex, NULL); | 1046 | mutex_init(&client_list_mutex); |
1046 | } | 1047 | } |
1047 | 1048 | ||
1048 | void client_shutdown(void) | 1049 | void client_shutdown(void) |
@@ -1051,6 +1052,6 @@ void client_shutdown(void) | |||
1051 | FOREACH(struct mux_client *client, &client_list) { | 1052 | FOREACH(struct mux_client *client, &client_list) { |
1052 | client_close(client); | 1053 | client_close(client); |
1053 | } ENDFOREACH | 1054 | } ENDFOREACH |
1054 | pthread_mutex_destroy(&client_list_mutex); | 1055 | mutex_destroy(&client_list_mutex); |
1055 | collection_free(&client_list); | 1056 | collection_free(&client_list); |
1056 | } | 1057 | } |