+ struct timespec ts, cur;
+ int msec = timeout % 1000;
+ int ret;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+
+ ts.tv_nsec += msec * 1000000;
+ if (ts.tv_nsec > 1000000000) {
+ ts.tv_sec++;
+ ts.tv_nsec -= 1000000000;
+ }
+ ts.tv_sec += timeout / 1000;
+
+ while (1) {
+ ret = poll(fds, nfds, timeout);
+ if (ret == EAGAIN)
+ continue;
+
+ if (ret != EINTR)
+ return ret;
+
+ clock_gettime(CLOCK_MONOTONIC, &cur);
+ timeout = (ts.tv_sec - cur.tv_sec) * 1000;
+ timeout += (ts.tv_nsec - cur.tv_nsec) / 1000000;
+ if (timeout <= 0)
+ return 0;
+ }
+}
+
+int usock_inet_timeout(int type, const char *host, const char *service,
+ void *addr, int timeout)
+{
+ int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
+ bool server = !!(type & USOCK_SERVER);