mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 15:38:22 +00:00
Fix select() on Linux
This fixes a bug where the caller's timeval will be clobbered on Linux. The Kernel ABI *always* modifies the timeout argument but POSIX says it should be a const parameter. The wrapper now handles the difference and sys_select() may be used if obtaining the remainder on Linux is needed.
This commit is contained in:
parent
dfabcd84c1
commit
0f1ead8943
2 changed files with 17 additions and 6 deletions
|
@ -212,7 +212,7 @@ static bool ShouldUseSpinNanosleep(int clock, int flags,
|
||||||
* while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &abs, 0));
|
* while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &abs, 0));
|
||||||
*
|
*
|
||||||
* will accurately spin on `EINTR` errors. That way you're not impeding
|
* will accurately spin on `EINTR` errors. That way you're not impeding
|
||||||
* signal delivery and you're not losing precision on your wait timeout.
|
* signal delivery and you're not loosing precision on the wait timeout.
|
||||||
* This function has first-class support on Linux, FreeBSD, and NetBSD;
|
* This function has first-class support on Linux, FreeBSD, and NetBSD;
|
||||||
* on OpenBSD it's good; on XNU it's bad; and on Windows it's ugly.
|
* on OpenBSD it's good; on XNU it's bad; and on Windows it's ugly.
|
||||||
*
|
*
|
||||||
|
|
|
@ -43,26 +43,37 @@
|
||||||
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
|
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
|
||||||
struct timeval *timeout) {
|
struct timeval *timeout) {
|
||||||
int rc;
|
int rc;
|
||||||
|
struct timeval tv, *tvp;
|
||||||
POLLTRACE("select(%d, %p, %p, %p, %s) → ...", nfds, readfds, writefds,
|
POLLTRACE("select(%d, %p, %p, %p, %s) → ...", nfds, readfds, writefds,
|
||||||
exceptfds, DescribeTimeval(0, timeout));
|
exceptfds, DescribeTimeval(0, timeout));
|
||||||
|
|
||||||
|
// the linux kernel modifies timeout
|
||||||
|
if (timeout) {
|
||||||
|
if (IsAsan() && !__asan_is_valid(timeout, sizeof(*timeout))) {
|
||||||
|
return efault();
|
||||||
|
}
|
||||||
|
tv = *timeout;
|
||||||
|
tvp = &tv;
|
||||||
|
} else {
|
||||||
|
tvp = 0;
|
||||||
|
}
|
||||||
|
|
||||||
BEGIN_CANCELLATION_POINT;
|
BEGIN_CANCELLATION_POINT;
|
||||||
if (nfds < 0) {
|
if (nfds < 0) {
|
||||||
rc = einval();
|
rc = einval();
|
||||||
} else if (IsAsan() &&
|
} else if (IsAsan() &&
|
||||||
((readfds && !__asan_is_valid(readfds, FD_SIZE(nfds))) ||
|
((readfds && !__asan_is_valid(readfds, FD_SIZE(nfds))) ||
|
||||||
(writefds && !__asan_is_valid(writefds, FD_SIZE(nfds))) ||
|
(writefds && !__asan_is_valid(writefds, FD_SIZE(nfds))) ||
|
||||||
(exceptfds && !__asan_is_valid(exceptfds, FD_SIZE(nfds))) ||
|
(exceptfds && !__asan_is_valid(exceptfds, FD_SIZE(nfds))))) {
|
||||||
(timeout && !__asan_is_valid(timeout, sizeof(*timeout))))) {
|
|
||||||
rc = efault();
|
rc = efault();
|
||||||
} else if (!IsWindows()) {
|
} else if (!IsWindows()) {
|
||||||
rc = sys_select(nfds, readfds, writefds, exceptfds, timeout);
|
rc = sys_select(nfds, readfds, writefds, exceptfds, tvp);
|
||||||
} else {
|
} else {
|
||||||
rc = sys_select_nt(nfds, readfds, writefds, exceptfds, timeout, 0);
|
rc = sys_select_nt(nfds, readfds, writefds, exceptfds, tvp, 0);
|
||||||
}
|
}
|
||||||
END_CANCELLATION_POINT;
|
END_CANCELLATION_POINT;
|
||||||
|
|
||||||
POLLTRACE("select(%d, %p, %p, %p, [%s]) → %d% m", nfds, readfds, writefds,
|
POLLTRACE("select(%d, %p, %p, %p, [%s]) → %d% m", nfds, readfds, writefds,
|
||||||
exceptfds, DescribeTimeval(rc, timeout), rc);
|
exceptfds, DescribeTimeval(rc, tvp), rc);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue