2018-05-16 08:22:34 +00:00
|
|
|
#
|
2019-02-06 14:49:11 +00:00
|
|
|
# Chronos SendFile
|
|
|
|
# (c) Copyright 2018-Present
|
2018-05-16 08:22:34 +00:00
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
|
|
|
|
## This module provides cross-platform wrapper for ``sendfile()`` syscall.
|
|
|
|
|
2023-06-05 20:21:50 +00:00
|
|
|
{.push raises: [].}
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
|
2018-05-16 08:22:34 +00:00
|
|
|
when defined(nimdoc):
|
2018-10-25 19:59:40 +00:00
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
2018-05-16 08:22:34 +00:00
|
|
|
## Copies data between file descriptor ``infd`` and ``outfd``. Because this
|
|
|
|
## copying is done within the kernel, ``sendfile()`` is more efficient than
|
|
|
|
## the combination of ``read(2)`` and ``write(2)``, which would require
|
|
|
|
## transferring data to and from user space.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## ``infd`` should be a file descriptor opened for reading and
|
|
|
|
## ``outfd`` should be a descriptor opened for writing.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## The ``infd`` argument must correspond to a file which supports
|
|
|
|
## ``mmap(2)``-like operations (i.e., it cannot be a socket).
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## ``offset`` the file offset from which ``sendfile()`` will start reading
|
|
|
|
## data from ``infd``.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## ``count`` is the number of bytes to copy between the file descriptors.
|
2018-10-25 19:59:40 +00:00
|
|
|
## On exit ``count`` will hold number of bytes actually transferred between
|
2021-12-08 10:35:27 +00:00
|
|
|
## file descriptors. May be >0 even in the case of error return, if some
|
|
|
|
## bytes were sent before the error occurred.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## If the transfer was successful, the number of bytes written to ``outfd``
|
2021-05-07 20:52:24 +00:00
|
|
|
## is stored in ``count``, and ``0`` returned. Note that a successful call
|
|
|
|
## to ``sendfile()`` may write fewer bytes than requested; the caller should
|
2018-10-25 19:59:40 +00:00
|
|
|
## be prepared to retry the call if there were unsent bytes.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## On error, ``-1`` is returned.
|
2023-08-01 09:56:08 +00:00
|
|
|
elif defined(emscripten):
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2023-08-01 09:56:08 +00:00
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
|
|
|
raiseAssert "sendfile() is not implemented yet"
|
|
|
|
|
|
|
|
elif (defined(linux) or defined(android)) and not(defined(emscripten)):
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
proc osSendFile*(outfd, infd: cint, offset: ptr int, count: int): int
|
|
|
|
{.importc: "sendfile", header: "<sys/sendfile.h>".}
|
|
|
|
|
2018-10-25 19:59:40 +00:00
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
2018-05-16 08:22:34 +00:00
|
|
|
var o = offset
|
2021-12-08 10:35:27 +00:00
|
|
|
let res = osSendFile(cint(outfd), cint(infd), addr o, count)
|
|
|
|
if res >= 0:
|
|
|
|
count = res
|
|
|
|
0
|
|
|
|
else:
|
|
|
|
count = 0
|
|
|
|
-1
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
elif defined(freebsd) or defined(openbsd) or defined(netbsd) or
|
|
|
|
defined(dragonflybsd):
|
2023-05-15 16:45:26 +00:00
|
|
|
import oserrno
|
2018-05-16 08:22:34 +00:00
|
|
|
type
|
2023-05-15 16:45:26 +00:00
|
|
|
SendfileHeader* {.importc: "struct sf_hdtr",
|
2019-06-04 16:51:35 +00:00
|
|
|
header: """#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>""",
|
|
|
|
pure, final.} = object
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2018-05-21 21:52:57 +00:00
|
|
|
proc osSendFile*(outfd, infd: cint, offset: uint, size: uint,
|
|
|
|
hdtr: ptr SendfileHeader, sbytes: ptr uint,
|
2018-05-16 08:22:34 +00:00
|
|
|
flags: int): int {.importc: "sendfile",
|
|
|
|
header: """#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>""".}
|
|
|
|
|
2018-10-25 19:59:40 +00:00
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
2018-05-21 21:52:57 +00:00
|
|
|
var o = 0'u
|
2023-05-15 16:45:26 +00:00
|
|
|
let res = osSendFile(cint(infd), cint(outfd), uint(offset), uint(count),
|
|
|
|
nil, addr o, 0)
|
2021-12-08 10:35:27 +00:00
|
|
|
if res >= 0:
|
2018-10-25 19:59:40 +00:00
|
|
|
count = int(o)
|
2021-12-08 10:35:27 +00:00
|
|
|
0
|
2018-05-21 21:52:57 +00:00
|
|
|
else:
|
2018-10-25 19:59:40 +00:00
|
|
|
let err = osLastError()
|
2021-12-08 10:35:27 +00:00
|
|
|
count =
|
2023-05-15 16:45:26 +00:00
|
|
|
if err == EAGAIN: int(o)
|
2021-12-08 10:35:27 +00:00
|
|
|
else: 0
|
|
|
|
-1
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
elif defined(macosx):
|
2023-05-15 16:45:26 +00:00
|
|
|
import oserrno
|
|
|
|
|
2018-05-16 08:22:34 +00:00
|
|
|
type
|
2021-02-02 15:10:07 +00:00
|
|
|
SendfileHeader* {.importc: "struct sf_hdtr",
|
2019-06-04 16:51:35 +00:00
|
|
|
header: """#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>""",
|
|
|
|
pure, final.} = object
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
proc osSendFile*(fd, s: cint, offset: int, size: ptr int,
|
2018-05-21 21:52:57 +00:00
|
|
|
hdtr: ptr SendfileHeader,
|
2018-05-16 08:22:34 +00:00
|
|
|
flags: int): int {.importc: "sendfile",
|
|
|
|
header: """#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>""".}
|
|
|
|
|
2018-10-25 19:59:40 +00:00
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
2018-05-21 21:52:57 +00:00
|
|
|
var o = count
|
2021-12-08 10:35:27 +00:00
|
|
|
let res = osSendFile(cint(infd), cint(outfd), offset, addr o, nil, 0)
|
|
|
|
if res >= 0:
|
2018-10-25 19:59:40 +00:00
|
|
|
count = int(o)
|
2021-12-08 10:35:27 +00:00
|
|
|
0
|
2018-05-21 21:52:57 +00:00
|
|
|
else:
|
2018-10-25 19:59:40 +00:00
|
|
|
let err = osLastError()
|
2021-12-08 10:35:27 +00:00
|
|
|
count =
|
2023-05-15 16:45:26 +00:00
|
|
|
if err == EAGAIN: int(o)
|
2021-12-08 10:35:27 +00:00
|
|
|
else: 0
|
|
|
|
-1
|