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 @@
#include <syslog.h>
#include "log.h"
+#include "utils.h"
unsigned int log_level = LL_WARNING;
@@ -70,7 +71,7 @@ void usbmuxd_log(enum loglevel level, const char *fmt, ...)
if(level > log_level)
return;
- gettimeofday(&ts, NULL);
+ get_tick_count(&ts);
tp = localtime(&ts.tv_sec);
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 @@
#include "usb.h"
#include "log.h"
#include "device.h"
+#include "utils.h"
// interval for device connection/disconnection polling, in milliseconds
// we need this because there is currently no asynchronous device discovery mechanism in libusb
@@ -266,7 +267,7 @@ int usb_discover(void)
usbmuxd_log(LL_FATAL, "Too many errors getting device list");
return cnt;
} else {
- gettimeofday(&next_dev_poll_time, NULL);
+ get_tick_count(&next_dev_poll_time);
next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000;
next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000;
next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000;
@@ -477,7 +478,7 @@ int usb_discover(void)
libusb_free_device_list(devs, 1);
- gettimeofday(&next_dev_poll_time, NULL);
+ get_tick_count(&next_dev_poll_time);
next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000;
next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000;
next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000;
@@ -538,7 +539,7 @@ static int dev_poll_remain_ms(void)
struct timeval tv;
if(!device_polling)
return 100000; // devices will never be polled if this is > 0
- gettimeofday(&tv, NULL);
+ get_tick_count(&tv);
msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000;
msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000;
if(msecs < 0)
@@ -595,7 +596,7 @@ int usb_process_timeout(int msec)
{
int res;
struct timeval tleft, tcur, tfin;
- gettimeofday(&tcur, NULL);
+ get_tick_count(&tcur);
tfin.tv_sec = tcur.tv_sec + (msec / 1000);
tfin.tv_usec = tcur.tv_usec + (msec % 1000) * 1000;
tfin.tv_sec += tfin.tv_usec / 1000000;
@@ -614,7 +615,7 @@ int usb_process_timeout(int msec)
}
// reap devices marked dead due to an RX error
reap_dead_devices();
- gettimeofday(&tcur, NULL);
+ get_tick_count(&tcur);
}
return 0;
}
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 @@
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
+#include <time.h>
#include <sys/time.h>
#include "utils.h"
@@ -298,15 +299,26 @@ int plist_write_to_filename(plist_t plist, const char *filename, enum plist_form
return 1;
}
+void get_tick_count(struct timeval * tv)
+{
+ struct timespec ts;
+ if(0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
+ tv->tv_sec = ts.tv_sec;
+ tv->tv_usec = ts.tv_nsec / 1000;
+ } else {
+ gettimeofday(tv, NULL);
+ }
+}
+
/**
* Get number of milliseconds since the epoch.
*/
uint64_t mstime64(void)
{
struct timeval tv;
- gettimeofday(&tv, NULL);
+ get_tick_count(&tv);
- // Careful, avoid overflow on 32 bit systems
- // time_t could be 4 bytes
+ // Careful, avoid overflow on 32 bit systems
+ // time_t could be 4 bytes
return ((long long)tv.tv_sec) * 1000LL + ((long long)tv.tv_usec) / 1000LL;
}
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);
int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format);
uint64_t mstime64(void);
+void get_tick_count(struct timeval * tv);
#endif