Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken poll and nfds_t bindings #24331

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/posix/posix.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,21 @@ proc setprotoent*(a1: cint) {.importc, header: "<netdb.h>".}
proc setservent*(a1: cint) {.importc, header: "<netdb.h>".}

when not defined(lwip):
proc poll*(a1: ptr TPollfd, a2: Tnfds, a3: int): cint {.
# Linux and Haiku emulate SVR4, which used unsigned long.
# Meanwhile, BSD derivatives had used unsigned int; we will use this
# for the else case, because it is more widely cloned than SVR4's
# behavior.
when defined(linux) or defined(haiku):
type
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = culong
elif defined(zephyr):
type
Tnfds* = distinct cint
else:
type
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = cuint

proc poll*(a1: ptr TPollfd, a2: Tnfds, a3: cint): cint {.
importc, header: "<poll.h>", sideEffect.}

proc realpath*(name, resolved: cstring): cstring {.
Expand Down
2 changes: 0 additions & 2 deletions lib/posix/posix_haiku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,6 @@ type
events*: cshort ## The input event flags (see below).
revents*: cshort ## The output event flags (see below).

Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = culong

var
errno* {.importc, header: "<errno.h>".}: cint ## error variable
h_errno* {.importc, header: "<netdb.h>".}: cint
Expand Down
2 changes: 0 additions & 2 deletions lib/posix/posix_linux_amd64.nim
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,6 @@ type
events*: cshort ## The input event flags (see below).
revents*: cshort ## The output event flags (see below).

Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = culong

var
errno* {.importc, header: "<errno.h>".}: cint ## error variable
h_errno* {.importc, header: "<netdb.h>".}: cint
Expand Down
2 changes: 0 additions & 2 deletions lib/posix/posix_macos_amd64.nim
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,6 @@ type
events*: cshort ## The input event flags (see below).
revents*: cshort ## The output event flags (see below).

Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = cint

var
errno* {.importc, header: "<errno.h>".}: cint ## error variable
h_errno* {.importc, header: "<netdb.h>".}: cint
Expand Down
2 changes: 0 additions & 2 deletions lib/posix/posix_nintendoswitch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,6 @@ type
events*: cshort ## The input event flags (see below).
revents*: cshort ## The output event flags (see below).

Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = culong

var
errno* {.importc, header: "<errno.h>".}: cint ## error variable
h_errno* {.importc, header: "<netdb.h>".}: cint
Expand Down
2 changes: 0 additions & 2 deletions lib/posix/posix_openbsd_amd64.nim
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,6 @@ type
events*: cshort ## The input event flags (see below).
revents*: cshort ## The output event flags (see below).

Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = cint

var
errno* {.importc, header: "<errno.h>".}: cint ## error variable
h_errno* {.importc, header: "<netdb.h>".}: cint
Expand Down
7 changes: 0 additions & 7 deletions lib/posix/posix_other.nim
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,6 @@ when not defined(lwip):
events*: cshort ## The input event flags (see below).
revents*: cshort ## The output event flags (see below).

when defined(zephyr):
type
Tnfds* = distinct cint
else:
type
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = cint

var
errno* {.importc, header: "<errno.h>".}: cint ## error variable
h_errno* {.importc, header: "<netdb.h>".}: cint
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/ioselects/ioselectors_poll.nim
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ proc selectInto*[T](s: Selector[T], timeout: int,
verifySelectParams(timeout)

s.withPollLock():
let count = posix.poll(addr(s.pollfds[0]), Tnfds(s.pollcnt), timeout)
let count = posix.poll(addr(s.pollfds[0]), Tnfds(s.pollcnt), cint(timeout))
if count < 0:
result = 0
let err = osLastError()
Expand Down
6 changes: 3 additions & 3 deletions lib/pure/net.nim
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ when defined(nimHasStyleChecks):
when defined(posix) and not defined(lwip):
from std/posix import TPollfd, POLLIN, POLLPRI, POLLOUT, POLLWRBAND, Tnfds

template monitorPollEvent(x: var SocketHandle, y: cint, timeout: int): int =
template monitorPollEvent(x: var SocketHandle, y, timeout: cint): int =
var tpollfd: TPollfd
tpollfd.fd = cast[cint](x)
tpollfd.events = y
Expand All @@ -222,14 +222,14 @@ proc timeoutRead(fd: var SocketHandle, timeout = 500): int =
var fds = @[fd]
selectRead(fds, timeout)
else:
monitorPollEvent(fd, POLLIN or POLLPRI, timeout)
monitorPollEvent(fd, POLLIN or POLLPRI, cint(timeout))

proc timeoutWrite(fd: var SocketHandle, timeout = 500): int =
when defined(windows) or defined(lwip):
var fds = @[fd]
selectWrite(fds, timeout)
else:
monitorPollEvent(fd, POLLOUT or POLLWRBAND, timeout)
monitorPollEvent(fd, POLLOUT or POLLWRBAND, cint(timeout))

proc socketError*(socket: Socket, err: int = -1, async = false,
lastError = (-1).OSErrorCode,
Expand Down