commit 40b07de56edb98d655097d8a82e76bfb8965d5c0
parent 3ab10c46797e001ac6261e27a3a93b53572fa3d4
Author: Egor <egor@opensrc.club>
Date: Tue, 10 May 2022 20:03:51 +0300
net: invert the meaning of CLOEXEC in sockflags
Currently if you want a file descriptor without CLOEXEC, you have to
pass a dummy 0 argument. And if you pass NONBLOCK, you have to remember
to also pass CLOEXEC, since in most cases that's what you actually want.
This will also make it easier to add socketflags to connect/socket
functions, since they already take sockopts and so you can't just
len(flags) == 0 to detect that no SOCK flags have been specified.
Signed-off-by: Egor <egor@opensrc.club>
Diffstat:
2 files changed, 8 insertions(+), 14 deletions(-)
diff --git a/net/+freebsd.ha b/net/+freebsd.ha
@@ -12,27 +12,24 @@ use strings;
// Optional flags to [[accept]] to be set on the returned [[io::file]].
// See the O_CLOEXEC and O_NONBLOCK sections of open(2) for details.
+// Note that CLOEXEC is on by default, and NOCLOEXEC flag disables it.
export type sockflags = enum int {
- CLOEXEC = rt::SOCK_CLOEXEC,
+ NOCLOEXEC = rt::SOCK_CLOEXEC,
NONBLOCK = rt::SOCK_NONBLOCK
};
// Accepts the next connection from a socket. Blocks until a new connection is
-// available. Optionally accepts CLOEXEC and NONBLOCK flags. If flags are
+// available. Optionally accepts NOCLOEXEC and NONBLOCK flags. If flags are
// supplied, the [[io::file]] returned will have the supplied flags set.
-// If no flags are supplied, CLOEXEC is used by default.
export fn accept(sock: io::file, flags: sockflags...) (io::file | error) = {
let sn = rt::sockaddr {...};
const sz = size(rt::sockaddr): u32;
- // Default to CLOEXEC if no flags are supplied
- if (len(flags) == 0) {
- flags = [sockflags::CLOEXEC];
- };
// Apply any supplied flags
let f = 0i;
for (let i = 0z; i < len(flags); i += 1) {
f |= flags[i];
};
+ f ^= rt::SOCK_CLOEXEC; // invert CLOEXEC
const fd = match (rt::accept4(sock, &sn, &sz, f)) {
case let err: rt::errno =>
return errors::errno(err);
diff --git a/net/+linux.ha b/net/+linux.ha
@@ -12,27 +12,24 @@ use strings;
// Optional flags to [[accept]] to be set on the returned [[io::file]].
// See the O_CLOEXEC and O_NONBLOCK sections of open(2) for details.
+// Note that CLOEXEC is on by default, and NOCLOEXEC flag disables it.
export type sockflags = enum int {
- CLOEXEC = rt::SOCK_CLOEXEC,
+ NOCLOEXEC = rt::SOCK_CLOEXEC,
NONBLOCK = rt::SOCK_NONBLOCK
};
// Accepts the next connection from a socket. Blocks until a new connection is
-// available. Optionally accepts CLOEXEC and NONBLOCK flags. If flags are
+// available. Optionally accepts NOCLOEXEC and NONBLOCK flags. If flags are
// supplied, the [[io::file]] returned will have the supplied flags set.
-// If no flags are supplied, CLOEXEC is used by default.
export fn accept(sock: io::file, flags: sockflags...) (io::file | error) = {
let sn = rt::sockaddr {...};
const sz = size(rt::sockaddr): u32;
- // Default to CLOEXEC if no flags are supplied
- if (len(flags) == 0) {
- flags = [sockflags::CLOEXEC];
- };
// Apply any supplied flags
let f = 0i;
for (let i = 0z; i < len(flags); i += 1) {
f |= flags[i];
};
+ f ^= rt::SOCK_CLOEXEC; // invert CLOEXEC
const fd = match (rt::accept4(sock, &sn, &sz, f: int)) {
case let err: rt::errno =>
return errors::errno(err);