summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Martin Szulecki2014-10-31 12:52:23 +0100
committerGravatar Nikias Bassen2014-11-11 13:35:00 +0100
commit04e07442bbc8a5d8515fa1eea52cb15ebd2cc992 (patch)
treef8045818312a6e07996e1adcada42b7391237973
parent50cb34766753f9ad6a046bdc3e1fa1f1a1aacc73 (diff)
downloadusbmuxd-04e07442bbc8a5d8515fa1eea52cb15ebd2cc992.tar.gz
usbmuxd-04e07442bbc8a5d8515fa1eea52cb15ebd2cc992.tar.bz2
Use new get_tick_count() to avoid timing issues on packets
-rw-r--r--src/log.c3
-rw-r--r--src/usb-linux.c11
-rw-r--r--src/utils.c18
-rw-r--r--src/utils.h1
4 files changed, 24 insertions, 9 deletions
diff --git a/src/log.c b/src/log.c
index 9ad09da..46839ee 100644
--- a/src/log.c
+++ b/src/log.c
@@ -31,6 +31,7 @@
31#include <syslog.h> 31#include <syslog.h>
32 32
33#include "log.h" 33#include "log.h"
34#include "utils.h"
34 35
35unsigned int log_level = LL_WARNING; 36unsigned int log_level = LL_WARNING;
36 37
@@ -70,7 +71,7 @@ void usbmuxd_log(enum loglevel level, const char *fmt, ...)
70 if(level > log_level) 71 if(level > log_level)
71 return; 72 return;
72 73
73 gettimeofday(&ts, NULL); 74 get_tick_count(&ts);
74 tp = localtime(&ts.tv_sec); 75 tp = localtime(&ts.tv_sec);
75 76
76 fs = malloc(20 + strlen(fmt)); 77 fs = malloc(20 + strlen(fmt));
diff --git a/src/usb-linux.c b/src/usb-linux.c
index 751b6ed..8acbace 100644
--- a/src/usb-linux.c
+++ b/src/usb-linux.c
@@ -34,6 +34,7 @@
34#include "usb.h" 34#include "usb.h"
35#include "log.h" 35#include "log.h"
36#include "device.h" 36#include "device.h"
37#include "utils.h"
37 38
38// interval for device connection/disconnection polling, in milliseconds 39// interval for device connection/disconnection polling, in milliseconds
39// we need this because there is currently no asynchronous device discovery mechanism in libusb 40// we need this because there is currently no asynchronous device discovery mechanism in libusb
@@ -266,7 +267,7 @@ int usb_discover(void)
266 usbmuxd_log(LL_FATAL, "Too many errors getting device list"); 267 usbmuxd_log(LL_FATAL, "Too many errors getting device list");
267 return cnt; 268 return cnt;
268 } else { 269 } else {
269 gettimeofday(&next_dev_poll_time, NULL); 270 get_tick_count(&next_dev_poll_time);
270 next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000; 271 next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000;
271 next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000; 272 next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000;
272 next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000; 273 next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000;
@@ -477,7 +478,7 @@ int usb_discover(void)
477 478
478 libusb_free_device_list(devs, 1); 479 libusb_free_device_list(devs, 1);
479 480
480 gettimeofday(&next_dev_poll_time, NULL); 481 get_tick_count(&next_dev_poll_time);
481 next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000; 482 next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000;
482 next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000; 483 next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000;
483 next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000; 484 next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000;
@@ -538,7 +539,7 @@ static int dev_poll_remain_ms(void)
538 struct timeval tv; 539 struct timeval tv;
539 if(!device_polling) 540 if(!device_polling)
540 return 100000; // devices will never be polled if this is > 0 541 return 100000; // devices will never be polled if this is > 0
541 gettimeofday(&tv, NULL); 542 get_tick_count(&tv);
542 msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000; 543 msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000;
543 msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000; 544 msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000;
544 if(msecs < 0) 545 if(msecs < 0)
@@ -595,7 +596,7 @@ int usb_process_timeout(int msec)
595{ 596{
596 int res; 597 int res;
597 struct timeval tleft, tcur, tfin; 598 struct timeval tleft, tcur, tfin;
598 gettimeofday(&tcur, NULL); 599 get_tick_count(&tcur);
599 tfin.tv_sec = tcur.tv_sec + (msec / 1000); 600 tfin.tv_sec = tcur.tv_sec + (msec / 1000);
600 tfin.tv_usec = tcur.tv_usec + (msec % 1000) * 1000; 601 tfin.tv_usec = tcur.tv_usec + (msec % 1000) * 1000;
601 tfin.tv_sec += tfin.tv_usec / 1000000; 602 tfin.tv_sec += tfin.tv_usec / 1000000;
@@ -614,7 +615,7 @@ int usb_process_timeout(int msec)
614 } 615 }
615 // reap devices marked dead due to an RX error 616 // reap devices marked dead due to an RX error
616 reap_dead_devices(); 617 reap_dead_devices();
617 gettimeofday(&tcur, NULL); 618 get_tick_count(&tcur);
618 } 619 }
619 return 0; 620 return 0;
620} 621}
diff --git a/src/utils.c b/src/utils.c
index 5dd871d..ceb65e1 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -28,6 +28,7 @@
28#include <string.h> 28#include <string.h>
29#include <stdio.h> 29#include <stdio.h>
30#include <stdarg.h> 30#include <stdarg.h>
31#include <time.h>
31#include <sys/time.h> 32#include <sys/time.h>
32 33
33#include "utils.h" 34#include "utils.h"
@@ -298,15 +299,26 @@ int plist_write_to_filename(plist_t plist, const char *filename, enum plist_form
298 return 1; 299 return 1;
299} 300}
300 301
302void get_tick_count(struct timeval * tv)
303{
304 struct timespec ts;
305 if(0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
306 tv->tv_sec = ts.tv_sec;
307 tv->tv_usec = ts.tv_nsec / 1000;
308 } else {
309 gettimeofday(tv, NULL);
310 }
311}
312
301/** 313/**
302 * Get number of milliseconds since the epoch. 314 * Get number of milliseconds since the epoch.
303 */ 315 */
304uint64_t mstime64(void) 316uint64_t mstime64(void)
305{ 317{
306 struct timeval tv; 318 struct timeval tv;
307 gettimeofday(&tv, NULL); 319 get_tick_count(&tv);
308 320
309 // Careful, avoid overflow on 32 bit systems 321 // Careful, avoid overflow on 32 bit systems
310 // time_t could be 4 bytes 322 // time_t could be 4 bytes
311 return ((long long)tv.tv_sec) * 1000LL + ((long long)tv.tv_usec) / 1000LL; 323 return ((long long)tv.tv_sec) * 1000LL + ((long long)tv.tv_usec) / 1000LL;
312} 324}
diff --git a/src/utils.h b/src/utils.h
index 00041a0..1137a93 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -87,5 +87,6 @@ int plist_read_from_filename(plist_t *plist, const char *filename);
87int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format); 87int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format);
88 88
89uint64_t mstime64(void); 89uint64_t mstime64(void);
90void get_tick_count(struct timeval * tv);
90 91
91#endif 92#endif