Skip to content

Commit

Permalink
accept-operation: use SOCK_* flags, not O_*
Browse files Browse the repository at this point in the history
That's how accept is defined and on some systems (OpenBSD), the
different flags do have different values.

As SOCK_CLOEXEC does exist on guile-2.2, we can use it by default
unconditionally.
  • Loading branch information
Lennart Jablonka committed Feb 16, 2025
1 parent 96cd1f4 commit ccd4250
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
17 changes: 7 additions & 10 deletions fibers.texi
Original file line number Diff line number Diff line change
Expand Up @@ -826,20 +826,17 @@ of these is implemented yet at time of writing.
In the meantime, don't change the ‘less deep’ waiter while
@code{with-read-waiter} / @code{with-write-waiter} is doing its thing!

@defun accept-operation port [flags=(logior O_NONBLOCK O_CLOEXEC)]
@defun accept-operation port [flags=(logior SOCK_NONBLOCK SOCK_CLOEXEC)]
Like @code{(accept port flags)}, but as an operation. Unlike
@code{accept}, @code{O_NONBLOCK} is included in the default flags,
@code{accept}, @code{SOCK_NONBLOCK} is included in the default flags,
which makes the accepted port non-blocking and hence suitable for
use with Fibers.

The flag @code{O_CLOEXEC} is also included (if available). Without
this flag, ports would be leaked to subprocesses by default, which
usually is at best inefficient and at worst a security problem. But
rarely, ports are intended to be leaked to subprocesses, in which
case you could remove this flag.

Fibers doesn't emulate @code{O_CLOEXEC} when unavailable, because it
would not be entirely equivalent in case of parallelism.
The flag @code{SOCK_CLOEXEC} is also included. Without this flag,
ports would be leaked to subprocesses by default, which usually is at
best inefficient and at worst a security problem. But rarely, ports
are intended to be leaked to subprocesses, in which case you could
remove this flag.
@end defun

@node REPL Commands
Expand Down
22 changes: 7 additions & 15 deletions fibers/io-wakeup.scm
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,17 @@ The write waiter at the current depth (relative to the invocation of
the returned thunk) may not be changed while the thunk is being invoked."
(with-x-waiting-is-failure port current-write-waiter try-fn))

(define O_CLOEXEC*
(if (defined? 'O_CLOEXEC)
O_CLOEXEC ; doesn't exist on guile-2.2
0))

(define* (accept-operation port #:key (flags (logior O_CLOEXEC* O_NONBLOCK)))
(define* (accept-operation port #:key (flags (logior SOCK_CLOEXEC SOCK_NONBLOCK)))
"Like '(accept port flags)', but as an operation. Unlike
'accept', 'O_NONBLOCK' is included in the default flags,
'accept', 'SOCK_NONBLOCK' is included in the default flags,
which makes the accepted port non-blocking and hence suitable for
use with Fibers.
The flag 'O_CLOEXEC' is also included (if available). Without
this flag, ports would be leaked to subprocesses by default, which
usually is at best inefficient and at worst a security problem. But
rarely, ports are intended to be leaked to subprocesses, in which
case you could remove this flag.
Fibers doesn't emulate 'O_CLOEXEC' when unavailable, because it
would not be entirely equivalent in case of parallelism."
The flag 'SOCK_CLOEXEC' is also included. Without this flag, ports
would be leaked to subprocesses by default, which usually is at best
inefficient and at worst a security problem. But rarely, ports are
intended to be leaked to subprocesses, in which case you could remove
this flag."
(define (try)
(let ((new (accept port flags)))
(and new (lambda () (values new)))))
Expand Down

0 comments on commit ccd4250

Please sign in to comment.