summaryrefslogtreecommitdiffstats
path: root/libusbmuxd/libusbmuxd.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2013-04-18 15:32:03 +0200
committerGravatar Nikias Bassen2013-04-18 15:32:03 +0200
commitf014642d1d551627aeb16db253b9ea1a5a767273 (patch)
tree76b972a03af0ec82c46021b93a2040f109504808 /libusbmuxd/libusbmuxd.c
parent9da79e5dc1ba779b13b2d0d1f59e19d015af7a3e (diff)
downloadusbmuxd-f014642d1d551627aeb16db253b9ea1a5a767273.tar.gz
usbmuxd-f014642d1d551627aeb16db253b9ea1a5a767273.tar.bz2
libusbmuxd: fix race conition in automatic usbmuxd_init invocation
Diffstat (limited to 'libusbmuxd/libusbmuxd.c')
-rw-r--r--libusbmuxd/libusbmuxd.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/libusbmuxd/libusbmuxd.c b/libusbmuxd/libusbmuxd.c
index 8470ddc..952e680 100644
--- a/libusbmuxd/libusbmuxd.c
+++ b/libusbmuxd/libusbmuxd.c
@@ -650,6 +650,7 @@ static void *device_monitor(void *data)
int usbmuxd_init()
{
int res = 0;
+ LOCK;
handle_events = 1;
#ifdef WIN32
devmon = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)device_monitor, NULL, 0, NULL);
@@ -662,16 +663,19 @@ int usbmuxd_init()
if (res != 0) {
DEBUG(1, "%s: ERROR: Could not start device watcher thread!\n", __func__);
handle_events = 0;
+ UNLOCK;
return res;
}
struct timespec ts = {0, 250000000};
nanosleep(&ts, NULL);
libusbmuxd_initialized = 1;
+ UNLOCK;
return 0;
}
int usbmuxd_deinit()
{
+ LOCK;
handle_events = 0;
shutdown_socket(listenfd, SHUT_RDWR);
@@ -687,6 +691,7 @@ int usbmuxd_deinit()
}
#endif
libusbmuxd_initialized = 0;
+ UNLOCK;
return 0;
}
@@ -698,7 +703,12 @@ int usbmuxd_subscribe(usbmuxd_event_cb_t callback, void *user_data)
event_cb = callback;
event_user_data = user_data;
- if (!libusbmuxd_initialized) {
+ int is_initialized = 0;
+ LOCK;
+ is_initialized = libusbmuxd_initialized;
+ UNLOCK;
+
+ if (!is_initialized) {
usbmuxd_init();
} else {
FOREACH(usbmuxd_device_info_t *dev, &devices) {
@@ -860,7 +870,12 @@ int usbmuxd_get_device_by_udid(const char *udid, usbmuxd_device_info_t *device)
return -EINVAL;
}
- if (!libusbmuxd_initialized) {
+ int is_initialized = 0;
+ LOCK;
+ is_initialized = libusbmuxd_initialized;
+ UNLOCK;
+
+ if (!is_initialized) {
usbmuxd_init();
}