forked from ValdikSS/skype-poll-fix
/
skype-poll-fix.c
85 lines (70 loc) · 1.9 KB
/
skype-poll-fix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <ctype.h>
#ifdef linux
#include <poll.h>
#endif
#ifdef __APPLE__
#include <sys/event.h>
#include <sys/time.h>
#endif
#define _STRINGIZE(x) \
#x
#define STRINGIZE(x) \
_STRINGIZE(x)
#if defined(linux)
# define POLL_FUNC_SIG struct pollfd *fds, nfds_t nfds, int timeout
# define POLL_FUNC_NAME poll
#elif defined(__APPLE__)
# define POLL_FUNC_SIG int kq, const struct kevent *changelist, int nchanges, \
struct kevent *eventlist, int nevents, \
const struct timespec *timeout
# define POLL_FUNC_NAME kevent
#else
# error "bad platform"
#endif // if defined(linux)
/***************************************************************************/
static int is_only_digits(const char *str) {
for ( ; *str; ++str) {
if ( !isdigit(*str) )
return 0;
}
return 1;
}
/***************************************************************************/
char* SET_POLL_C;
char* MIN_POLL_C;
int MIN_POLL = 300, SET_POLL = 300;
static __typeof__(&POLL_FUNC_NAME) pollmethod_orig = 0;
int POLL_FUNC_NAME(POLL_FUNC_SIG) {
if (pollmethod_orig == NULL) {
pollmethod_orig = dlsym(RTLD_NEXT, STRINGIZE(POLL_FUNC_NAME));
MIN_POLL_C = getenv("MIN_POLL");
SET_POLL_C = getenv("SET_POLL");
if (MIN_POLL_C && is_only_digits(MIN_POLL_C)) {
MIN_POLL = atoi(MIN_POLL_C);
}
if (SET_POLL_C && is_only_digits(SET_POLL_C)) {
SET_POLL = atoi(SET_POLL_C);
}
}
#ifdef linux
if (timeout>=0 && timeout < MIN_POLL) {
timeout = SET_POLL;
}
return pollmethod_orig(fds, nfds, timeout);
#endif
#ifdef __APPLE__
struct timespec new_timeout;
if (timeout->tv_nsec < MIN_POLL * 1000000) {
new_timeout.tv_nsec = SET_POLL * 1000000;
}
else {
new_timeout = *timeout;
}
return pollmethod_orig(kq, changelist, nchanges, eventlist, nevents, &new_timeout);
#endif
}
/***************************************************************************/