Fix import poison regression. (#388)

* Initial commit.

* One more linux fix.

* Eliminate posix from asyncloop.

* Fix Linux warnings.

* Fix MacOS issues.

* Fix BSD issues.

* Fix comma.

* Fix Windows issues.
This commit is contained in:
Eugene Kabanov 2023-05-15 19:45:26 +03:00 committed by GitHub
parent 8aa8ee8822
commit 956ae5af55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 279 additions and 234 deletions

View File

@ -16,7 +16,7 @@ else:
from nativesockets import Port
import std/[tables, strutils, heapqueue, options, deques]
import stew/results
import "."/[config, osdefs, osutils, timer]
import "."/[config, osdefs, oserrno, osutils, timer]
export Port
export timer, results
@ -152,11 +152,6 @@ elif defined(macosx) or defined(freebsd) or defined(netbsd) or
defined(openbsd) or defined(dragonfly) or defined(macos) or
defined(linux) or defined(android) or defined(solaris):
import "."/selectors2
import "."/oserrno
from posix import MSG_PEEK, MSG_NOSIGNAL,
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGBUS, SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2,
SIGPIPE, SIGALRM, SIGTERM, SIGPIPE
export SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGBUS, SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2,
SIGPIPE, SIGALRM, SIGTERM, SIGPIPE
@ -575,7 +570,7 @@ elif defined(macosx) or defined(freebsd) or defined(netbsd) or
proc globalInit() =
# We are ignoring SIGPIPE signal, because we are working with EPIPE.
posix.signal(cint(SIGPIPE), SIG_IGN)
signal(cint(SIGPIPE), SIG_IGN)
proc initAPI(disp: PDispatcher) =
discard
@ -635,7 +630,7 @@ elif defined(macosx) or defined(freebsd) or defined(netbsd) or
if not(isNil(adata.writer.function)):
newEvents.incl(Event.Write)
do:
return err(OSErrorCode(osdefs.EBADF))
return err(osdefs.EBADF)
loop.selector.updateHandle2(cint(fd), newEvents)
proc removeReader2*(fd: AsyncFD): Result[void, OSErrorCode] =
@ -648,7 +643,7 @@ elif defined(macosx) or defined(freebsd) or defined(netbsd) or
if not(isNil(adata.writer.function)):
newEvents.incl(Event.Write)
do:
return err(OSErrorCode(osdefs.EBADF))
return err(osdefs.EBADF)
loop.selector.updateHandle2(cint(fd), newEvents)
proc addWriter2*(fd: AsyncFD, cb: CallbackFunc,
@ -663,7 +658,7 @@ elif defined(macosx) or defined(freebsd) or defined(netbsd) or
if not(isNil(adata.reader.function)):
newEvents.incl(Event.Read)
do:
return err(OSErrorCode(osdefs.EBADF))
return err(osdefs.EBADF)
loop.selector.updateHandle2(cint(fd), newEvents)
proc removeWriter2*(fd: AsyncFD): Result[void, OSErrorCode] =
@ -676,7 +671,7 @@ elif defined(macosx) or defined(freebsd) or defined(netbsd) or
if not(isNil(adata.reader.function)):
newEvents.incl(Event.Read)
do:
return err(OSErrorCode(osdefs.EBADF))
return err(osdefs.EBADF)
loop.selector.updateHandle2(cint(fd), newEvents)
proc register*(fd: AsyncFD) {.raises: [Defect, OSError].} =
@ -743,7 +738,7 @@ elif defined(macosx) or defined(freebsd) or defined(netbsd) or
else:
OSErrorCode(0)
else:
OSErrorCode(osdefs.EBADF)
osdefs.EBADF
)
if not(isNil(aftercb)): aftercb(param)
@ -789,7 +784,7 @@ elif defined(macosx) or defined(freebsd) or defined(netbsd) or
withData(loop.selector, sigfd, adata) do:
adata.reader = AsyncCallback(function: cb, udata: udata)
do:
return err(OSErrorCode(osdefs.EBADF))
return err(osdefs.EBADF)
ok(sigfd)
proc addProcess2*(pid: int, cb: CallbackFunc,
@ -803,7 +798,7 @@ elif defined(macosx) or defined(freebsd) or defined(netbsd) or
withData(loop.selector, procfd, adata) do:
adata.reader = AsyncCallback(function: cb, udata: udata)
do:
return err(OSErrorCode(osdefs.EBADF))
return err(osdefs.EBADF)
ok(procfd)
proc removeSignal2*(sigfd: int): Result[void, OSErrorCode] =

View File

@ -40,7 +40,7 @@ proc getVirtualId[T](s: Selector[T]): SelectResult[int32] =
ok(s.virtualHoles.popLast())
else:
if s.virtualId == low(int32):
err(OSErrorCode(EMFILE))
err(EMFILE)
else:
dec(s.virtualId)
ok(s.virtualId)

View File

@ -10,6 +10,11 @@ import oserrno
export oserrno
when defined(windows):
from std/winlean import Sockaddr_storage, InAddr, In6Addr, Sockaddr_in,
Sockaddr_in6, SockLen, SockAddr, AddrInfo,
SocketHandle
export Sockaddr_storage, InAddr, In6Addr, Sockaddr_in, Sockaddr_in6, SockLen,
SockAddr, AddrInfo, SocketHandle
# Prerequisites for constants
template WSAIORW*(x, y): untyped = (IOC_INOUT or x or y)
template WSAIOW*(x, y): untyped =
@ -17,49 +22,6 @@ when defined(windows):
((clong(sizeof(int32)) and clong(IOCPARM_MASK)) shl 16) or (x shl 8) or y
type
Sockaddr_storage* {.final, pure.} = object
ss_family*: uint16
ss_pad1: array[6, byte]
ss_align: int64
ss_pad2: array[112, byte]
InAddr* {.final, pure, union.} = object
s_addr*: uint32
In6Addr* {.final, pure, union.} = object
s_addr*: array[16, byte]
Sockaddr_in* {.final, pure.} = object
sin_family*: uint16
sin_port*: uint16
sin_addr*: InAddr
sin_zero*: array[0..7, char]
Sockaddr_in6* {.final, pure.} = object
sin6_family*: uint16
sin6_port*: uint16
sin6_flowinfo*: uint32
sin6_addr*: In6Addr
sin6_scope_id*: uint32
SockLen* = cuint
SockAddr* {.final, pure.} = object
sa_family*: uint16
sa_data*: array[14, char]
AddrInfo* {.final, pure.} = object
ai_flags*: cint ## Input flags.
ai_family*: cint ## Address family of socket.
ai_socktype*: cint ## Socket type.
ai_protocol*: cint ## Protocol of socket.
ai_addrlen*: csize_t ## Length of socket address.
ai_canonname*: pointer ## Canonical name of service location.
ai_addr*: ptr SockAddr ## Socket address of socket.
ai_next*: ptr AddrInfo ## Pointer to next in list.
SocketHandle* = distinct int
HANDLE* = distinct uint
GUID* {.final, pure.} = object
D1*: uint32
@ -860,8 +822,43 @@ when defined(windows):
)
elif defined(macos) or defined(macosx):
import std/posix
export posix
from std/posix import close, shutdown, socket, getpeername, getsockname,
recvfrom, sendto, send, bindSocket, recv, connect,
unlink, listen, getaddrinfo, gai_strerror, getrlimit,
setrlimit, getpid, pthread_sigmask, sigemptyset,
sigaddset, sigismember, fcntl, accept, pipe, write,
signal, read, setsockopt, getsockopt,
Timeval, Timespec, Pid, Mode, Time, Sigset, SockAddr,
SockLen, Sockaddr_storage, Sockaddr_in, Sockaddr_in6,
Sockaddr_un, SocketHandle, AddrInfo, RLimit,
F_GETFL, F_SETFL, F_GETFD, F_SETFD, FD_CLOEXEC,
O_NONBLOCK, SOL_SOCKET, SOCK_RAW, MSG_NOSIGNAL,
AF_INET, AF_INET6, SO_ERROR, SO_REUSEADDR,
SO_REUSEPORT, SO_BROADCAST, IPPROTO_IP,
IPV6_MULTICAST_HOPS, SOCK_DGRAM, RLIMIT_NOFILE,
SIG_BLOCK, SIG_UNBLOCK,
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGBUS, SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2,
SIGPIPE, SIGALRM, SIGTERM, SIGPIPE, SIGCHLD
export close, shutdown, socket, getpeername, getsockname,
recvfrom, sendto, send, bindSocket, recv, connect,
unlink, listen, getaddrinfo, gai_strerror, getrlimit,
setrlimit, getpid, pthread_sigmask, sigemptyset,
sigaddset, sigismember, fcntl, accept, pipe, write,
signal, read, setsockopt, getsockopt,
Timeval, Timespec, Pid, Mode, Time, Sigset, SockAddr,
SockLen, Sockaddr_storage, Sockaddr_in, Sockaddr_in6,
Sockaddr_un, SocketHandle, AddrInfo, RLimit,
F_GETFL, F_SETFL, F_GETFD, F_SETFD, FD_CLOEXEC,
O_NONBLOCK, SOL_SOCKET, SOCK_RAW, MSG_NOSIGNAL,
AF_INET, AF_INET6, SO_ERROR, SO_REUSEADDR,
SO_REUSEPORT, SO_BROADCAST, IPPROTO_IP,
IPV6_MULTICAST_HOPS, SOCK_DGRAM, RLIMIT_NOFILE,
SIG_BLOCK, SIG_UNBLOCK,
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGBUS, SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2,
SIGPIPE, SIGALRM, SIGTERM, SIGPIPE, SIGCHLD
type
MachTimebaseInfo* {.importc: "struct mach_timebase_info",
@ -879,8 +876,47 @@ elif defined(macos) or defined(macosx):
importc, header: "<mach/mach_time.h>".}
elif defined(linux):
import std/[posix]
export posix
from std/posix import close, shutdown, sigemptyset, sigaddset, sigismember,
sigdelset, write, read, waitid, getaddrinfo,
gai_strerror, setsockopt, getsockopt, socket,
getrlimit, setrlimit, getpeername, getsockname,
recvfrom, sendto, send, bindSocket, recv, connect,
unlink, listen, sendmsg, recvmsg, getpid, fcntl,
pthread_sigmask, clock_gettime, signal,
ClockId, Itimerspec, Timespec, Sigset, Time, Pid, Mode,
SigInfo, Id, Tmsghdr, IOVec, RLimit,
SockAddr, SockLen, Sockaddr_storage, Sockaddr_in,
Sockaddr_in6, Sockaddr_un, AddrInfo, SocketHandle,
CLOCK_MONOTONIC, F_GETFL, F_SETFL, F_GETFD, F_SETFD,
FD_CLOEXEC, O_NONBLOCK, SIG_BLOCK, SIG_UNBLOCK,
SOL_SOCKET, SO_ERROR, RLIMIT_NOFILE, MSG_NOSIGNAL,
AF_INET, AF_INET6, SO_REUSEADDR, SO_REUSEPORT,
SO_BROADCAST, IPPROTO_IP, IPV6_MULTICAST_HOPS,
SOCK_DGRAM,
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGBUS, SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2,
SIGPIPE, SIGALRM, SIGTERM, SIGPIPE, SIGCHLD
export close, shutdown, sigemptyset, sigaddset, sigismember,
sigdelset, write, read, waitid, getaddrinfo,
gai_strerror, setsockopt, getsockopt, socket,
getrlimit, setrlimit, getpeername, getsockname,
recvfrom, sendto, send, bindSocket, recv, connect,
unlink, listen, sendmsg, recvmsg, getpid, fcntl,
pthread_sigmask, clock_gettime, signal,
ClockId, Itimerspec, Timespec, Sigset, Time, Pid, Mode,
SigInfo, Id, Tmsghdr, IOVec, RLimit,
SockAddr, SockLen, Sockaddr_storage, Sockaddr_in,
Sockaddr_in6, Sockaddr_un, AddrInfo, SocketHandle,
CLOCK_MONOTONIC, F_GETFL, F_SETFL, F_GETFD, F_SETFD,
FD_CLOEXEC, O_NONBLOCK, SIG_BLOCK, SIG_UNBLOCK,
SOL_SOCKET, SO_ERROR, RLIMIT_NOFILE, MSG_NOSIGNAL,
AF_INET, AF_INET6, SO_REUSEADDR, SO_REUSEPORT,
SO_BROADCAST, IPPROTO_IP, IPV6_MULTICAST_HOPS,
SOCK_DGRAM,
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGBUS, SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2,
SIGPIPE, SIGALRM, SIGTERM, SIGPIPE, SIGCHLD
when not defined(android) and defined(amd64):
const IP_MULTICAST_TTL*: cint = 33
@ -968,9 +1004,45 @@ elif defined(linux):
proc signalfd*(fd: cint, mask: var Sigset, flags: cint): cint {.
cdecl, importc: "signalfd", header: "<sys/signalfd.h>".}
else:
import std/posix
export posix
elif defined(freebsd) or defined(openbsd) or defined(netbsd) or
defined(dragonfly):
from std/posix import close, shutdown, socket, getpeername, getsockname,
recvfrom, sendto, send, bindSocket, recv, connect,
unlink, listen, getaddrinfo, gai_strerror, getrlimit,
setrlimit, getpid, pthread_sigmask, sigemptyset,
sigaddset, sigismember, fcntl, accept, pipe, write,
signal, read, setsockopt, getsockopt, clock_gettime,
Timeval, Timespec, Pid, Mode, Time, Sigset, SockAddr,
SockLen, Sockaddr_storage, Sockaddr_in, Sockaddr_in6,
Sockaddr_un, SocketHandle, AddrInfo, RLimit,
F_GETFL, F_SETFL, F_GETFD, F_SETFD, FD_CLOEXEC,
O_NONBLOCK, SOL_SOCKET, SOCK_RAW, MSG_NOSIGNAL,
AF_INET, AF_INET6, SO_ERROR, SO_REUSEADDR,
SO_REUSEPORT, SO_BROADCAST, IPPROTO_IP,
IPV6_MULTICAST_HOPS, SOCK_DGRAM, RLIMIT_NOFILE,
SIG_BLOCK, SIG_UNBLOCK, CLOCK_MONOTONIC,
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGBUS, SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2,
SIGPIPE, SIGALRM, SIGTERM, SIGPIPE, SIGCHLD
export close, shutdown, socket, getpeername, getsockname,
recvfrom, sendto, send, bindSocket, recv, connect,
unlink, listen, getaddrinfo, gai_strerror, getrlimit,
setrlimit, getpid, pthread_sigmask, sigemptyset,
sigaddset, sigismember, fcntl, accept, pipe, write,
signal, read, setsockopt, getsockopt, clock_gettime,
Timeval, Timespec, Pid, Mode, Time, Sigset, SockAddr,
SockLen, Sockaddr_storage, Sockaddr_in, Sockaddr_in6,
Sockaddr_un, SocketHandle, AddrInfo, RLimit,
F_GETFL, F_SETFL, F_GETFD, F_SETFD, FD_CLOEXEC,
O_NONBLOCK, SOL_SOCKET, SOCK_RAW, MSG_NOSIGNAL,
AF_INET, AF_INET6, SO_ERROR, SO_REUSEADDR,
SO_REUSEPORT, SO_BROADCAST, IPPROTO_IP,
IPV6_MULTICAST_HOPS, SOCK_DGRAM, RLIMIT_NOFILE,
SIG_BLOCK, SIG_UNBLOCK, CLOCK_MONOTONIC,
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGBUS, SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2,
SIGPIPE, SIGALRM, SIGTERM, SIGPIPE, SIGCHLD
var IP_MULTICAST_TTL* {.importc: "IP_MULTICAST_TTL",
header: "<netinet/in.h>".}: cint
@ -1021,9 +1093,6 @@ elif defined(dragonfly):
when defined(linux) or defined(macos) or defined(macosx) or defined(freebsd) or
defined(openbsd) or defined(netbsd) or defined(dragonfly):
export SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGBUS, SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2,
SIGPIPE, SIGALRM, SIGTERM, SIGPIPE
const
POSIX_SPAWN_RESETIDS* = 0x01
@ -1144,152 +1213,150 @@ when defined(linux) or defined(macos) or defined(macosx) or defined(freebsd) or
importc: "posix_spawnattr_setsigmask", header: "<spawn.h>",
sideEffect.}
when defined(posix):
when defined(linux):
const
P_PID* = cint(1)
WNOHANG* = cint(1)
WSTOPPED* = cint(2)
WEXITTED* = cint(4)
WNOWAIT* = cint(0x01000000)
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
(s and 0xFF00) shr 8
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
WAITEXITSTATUS(s)
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
(cast[int8](WSTATUS(s) + 1) shr 1) > 0
template WAITIFSTOPPED*(s: cint): bool =
(s and 0xFF) == 0x7F
template WAITIFCONTINUED*(s: cint): bool =
s == 0xFFFF
elif defined(openbsd):
const WNOHANG* = 1
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
(s shr 8) and 0xFF
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
WAITEXITSTATUS(s)
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
(WAITTERMSIG(s) != 0x7F) and (WSTATUS(s) != 0)
template WAITIFSTOPPED*(s: cint): bool =
WSTATUS(s) == 0x7F
template WAITIFCONTINUED*(s: cint): bool =
s == 0xFFFF
elif defined(dragonfly):
const WNOHANG* = 1
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
(s shr 8)
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
WAITEXITSTATUS(s)
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
(WAITTERMSIG(s) != 0x7F) and (WSTATUS(s) != 0)
template WAITIFSTOPPED*(s: cint): bool =
WSTATUS(s) == 0x7F
template WAITIFCONTINUED*(s: cint): bool =
s == 19
elif defined(netbsd):
const WNOHANG* = 1
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
(s shr 8) and 0xFF
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
WAITEXITSTATUS(s)
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
not(WAITIFSTOPPED(s)) and not(WAITIFCONTINUED(s)) and not(WAITIFEXITED(s))
template WAITIFSTOPPED*(s: cint): bool =
(WSTATUS(s) == 0x7F) and not(WAITIFCONTINUED(s))
template WAITIFCONTINUED*(s: cint): bool =
s == 0xFFFF
elif defined(freebsd):
const WNOHANG* = 1
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
s shr 8
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
s shr 8
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
let wstatus = WSTATUS(s)
(wstatus != 0x7F) and (wstatus != 0) and (s != 0x13)
template WAITIFSTOPPED*(s: cint): bool =
WSTATUS(s) == 0x7F
template WAITIFCONTINUED*(s: cint): bool =
x == 0x13
elif defined(macos) or defined(macosx):
const WNOHANG* = 1
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
(s shr 8) and 0xFF
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
s shr 8
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
let wstatus = WSTATUS(s)
(wstatus != 0x7F) and (wstatus != 0)
template WAITIFSTOPPED*(s: cint): bool =
(WSTATUS(s) == 0x7F) and (WAITSTOPSIG(s) != 0x13)
template WAITIFCONTINUED*(s: cint): bool =
(WSTATUS(s) == 0x7F) and (WAITSTOPSIG(s) == 0x13)
else:
proc WAITEXITSTATUS*(s: cint): cint {.
importc: "WEXITSTATUS", header: "<sys/wait.h>".}
## Exit code, iff WIFEXITED(s)
proc WAITTERMSIG*(s: cint): cint {.
importc: "WTERMSIG", header: "<sys/wait.h>".}
## Termination signal, iff WIFSIGNALED(s)
proc WAITSTOPSIG*(s: cint): cint {.
importc: "WSTOPSIG", header: "<sys/wait.h>".}
## Stop signal, iff WIFSTOPPED(s)
proc WAITIFEXITED*(s: cint): bool {.
importc: "WIFEXITED", header: "<sys/wait.h>".}
## True if child exited normally.
proc WAITIFSIGNALED*(s: cint): bool {.
importc: "WIFSIGNALED", header: "<sys/wait.h>".}
## True if child exited due to uncaught signal.
proc WAITIFSTOPPED*(s: cint): bool {.
importc: "WIFSTOPPED", header: "<sys/wait.h>".}
## True if child is currently stopped.
proc WAITIFCONTINUED*(s: cint): bool {.
importc: "WIFCONTINUED", header: "<sys/wait.h>".}
## True if child has been continued.
when defined(linux):
const
P_PID* = cint(1)
WNOHANG* = cint(1)
WSTOPPED* = cint(2)
WEXITED* = cint(4)
WNOWAIT* = cint(0x01000000)
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
(s and 0xFF00) shr 8
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
WAITEXITSTATUS(s)
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
(cast[int8](WSTATUS(s) + 1) shr 1) > 0
template WAITIFSTOPPED*(s: cint): bool =
(s and 0xFF) == 0x7F
template WAITIFCONTINUED*(s: cint): bool =
s == 0xFFFF
elif defined(openbsd):
const WNOHANG* = 1
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
(s shr 8) and 0xFF
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
WAITEXITSTATUS(s)
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
(WAITTERMSIG(s) != 0x7F) and (WSTATUS(s) != 0)
template WAITIFSTOPPED*(s: cint): bool =
WSTATUS(s) == 0x7F
template WAITIFCONTINUED*(s: cint): bool =
s == 0xFFFF
elif defined(dragonfly):
const WNOHANG* = 1
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
(s shr 8)
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
WAITEXITSTATUS(s)
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
(WAITTERMSIG(s) != 0x7F) and (WSTATUS(s) != 0)
template WAITIFSTOPPED*(s: cint): bool =
WSTATUS(s) == 0x7F
template WAITIFCONTINUED*(s: cint): bool =
s == 19
elif defined(netbsd):
const WNOHANG* = 1
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
(s shr 8) and 0xFF
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
WAITEXITSTATUS(s)
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
not(WAITIFSTOPPED(s)) and not(WAITIFCONTINUED(s)) and not(WAITIFEXITED(s))
template WAITIFSTOPPED*(s: cint): bool =
(WSTATUS(s) == 0x7F) and not(WAITIFCONTINUED(s))
template WAITIFCONTINUED*(s: cint): bool =
s == 0xFFFF
elif defined(freebsd):
const WNOHANG* = 1
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
s shr 8
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
s shr 8
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
let wstatus = WSTATUS(s)
(wstatus != 0x7F) and (wstatus != 0) and (s != 0x13)
template WAITIFSTOPPED*(s: cint): bool =
WSTATUS(s) == 0x7F
template WAITIFCONTINUED*(s: cint): bool =
x == 0x13
elif defined(macos) or defined(macosx):
const WNOHANG* = 1
template WSTATUS(s: cint): cint =
s and 0x7F
template WAITEXITSTATUS*(s: cint): cint =
(s shr 8) and 0xFF
template WAITTERMSIG*(s: cint): cint =
WSTATUS(s)
template WAITSTOPSIG*(s: cint): cint =
s shr 8
template WAITIFEXITED*(s: cint): bool =
WSTATUS(s) == 0
template WAITIFSIGNALED*(s: cint): bool =
let wstatus = WSTATUS(s)
(wstatus != 0x7F) and (wstatus != 0)
template WAITIFSTOPPED*(s: cint): bool =
(WSTATUS(s) == 0x7F) and (WAITSTOPSIG(s) != 0x13)
template WAITIFCONTINUED*(s: cint): bool =
(WSTATUS(s) == 0x7F) and (WAITSTOPSIG(s) == 0x13)
elif defined(posix):
proc WAITEXITSTATUS*(s: cint): cint {.
importc: "WEXITSTATUS", header: "<sys/wait.h>".}
## Exit code, iff WIFEXITED(s)
proc WAITTERMSIG*(s: cint): cint {.
importc: "WTERMSIG", header: "<sys/wait.h>".}
## Termination signal, iff WIFSIGNALED(s)
proc WAITSTOPSIG*(s: cint): cint {.
importc: "WSTOPSIG", header: "<sys/wait.h>".}
## Stop signal, iff WIFSTOPPED(s)
proc WAITIFEXITED*(s: cint): bool {.
importc: "WIFEXITED", header: "<sys/wait.h>".}
## True if child exited normally.
proc WAITIFSIGNALED*(s: cint): bool {.
importc: "WIFSIGNALED", header: "<sys/wait.h>".}
## True if child exited due to uncaught signal.
proc WAITIFSTOPPED*(s: cint): bool {.
importc: "WIFSTOPPED", header: "<sys/wait.h>".}
## True if child is currently stopped.
proc WAITIFCONTINUED*(s: cint): bool {.
importc: "WIFCONTINUED", header: "<sys/wait.h>".}
## True if child has been continued.
when defined(posix):
const
INVALID_SOCKET* = SocketHandle(-1)
INVALID_HANDLE_VALUE* = cint(-1)
proc `==`*(x: SocketHandle, y: int): bool =
x == SocketHandle(y)
proc `==`*(x: SocketHandle, y: int): bool = int(x) == y
when defined(macosx) or defined(macos) or defined(bsd):
const

View File

@ -59,9 +59,9 @@ elif defined(linux) or defined(android):
elif defined(freebsd) or defined(openbsd) or defined(netbsd) or
defined(dragonflybsd):
import posix, os
import oserrno
type
SendfileHeader* {.importc: "sf_hdtr",
SendfileHeader* {.importc: "struct sf_hdtr",
header: """#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>""",
@ -76,20 +76,21 @@ elif defined(freebsd) or defined(openbsd) or defined(netbsd) or
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
var o = 0'u
let res = osSendFile(cint(infd), cint(outfd), uint(offset), uint(count), nil,
addr o, 0)
let res = osSendFile(cint(infd), cint(outfd), uint(offset), uint(count),
nil, addr o, 0)
if res >= 0:
count = int(o)
0
else:
let err = osLastError()
count =
if int(err) == EAGAIN: int(o)
if err == EAGAIN: int(o)
else: 0
-1
elif defined(macosx):
import posix, os
import oserrno
type
SendfileHeader* {.importc: "struct sf_hdtr",
header: """#include <sys/types.h>
@ -113,6 +114,6 @@ elif defined(macosx):
else:
let err = osLastError()
count =
if int(err) == EAGAIN: int(o)
if err == EAGAIN: int(o)
else: 0
-1

View File

@ -454,21 +454,3 @@ func epochNanoSeconds*(moment: Moment): int64 =
proc fromNow*(t: typedesc[Moment], a: Duration): Moment {.inline.} =
## Returns moment in time which is equal to current moment + Duration.
Moment.now() + a
when defined(posix):
from posix import Time, Suseconds, Timeval, Timespec
func toTimeval*(a: Duration): Timeval =
## Convert Duration ``a`` to ``Timeval`` object.
let m = a.value mod Second.value
Timeval(
tv_sec: Time(a.value div Second.value),
tv_usec: Suseconds(m div Microsecond.value)
)
func toTimespec*(a: Duration): Timespec =
## Convert Duration ``a`` to ``Timespec`` object.
Timespec(
tv_sec: Time(a.value div Second.value),
tv_nsec: int(a.value mod Second.value)
)

View File

@ -21,7 +21,7 @@ from std/net import Domain, `==`, IpAddress, IpAddressFamily, parseIpAddress,
from std/nativesockets import toInt, `$`
export Domain, `==`, IpAddress, IpAddressFamily, parseIpAddress, SockType,
Protocol, Port, toInt, `$`, osdefs
Protocol, Port, toInt, `$`
const
DefaultStreamBufferSize* = 4096 ## Default buffer size for stream