hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 3ab10c46797e001ac6261e27a3a93b53572fa3d4
parent 70a2d3adea98d6b87b603f9ab458bac2d1a15b35
Author: Egor <egor@opensrc.club>
Date:   Tue, 10 May 2022 20:03:50 +0300

net: set CLOEXEC on new sockets atomically

Signed-off-by: Egor <egor@opensrc.club>

Diffstat:
Mnet/tcp/+freebsd.ha | 8++------
Mnet/tcp/+linux.ha | 8++------
Mnet/udp/+freebsd.ha | 6++----
Mnet/udp/+linux.ha | 6++----
Mnet/unix/+freebsd.ha | 8++------
Mnet/unix/+linux.ha | 8++------
Mnet/unix/socketpair.ha | 2+-
7 files changed, 13 insertions(+), 33 deletions(-)

diff --git a/net/tcp/+freebsd.ha b/net/tcp/+freebsd.ha @@ -22,14 +22,12 @@ export fn connect( case ip::addr6 => yield rt::AF_INET6: int; }; - const sockfd = match (rt::socket(family, rt::SOCK_STREAM, 0)) { + const sockfd = match (rt::socket(family, rt::SOCK_STREAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => yield fd; }; - let flags = rt::fcntl(sockfd, rt::F_GETFL, 0)!; - rt::fcntl(sockfd, rt::F_SETFL, flags | rt::O_CLOEXEC)!; for (let i = 0z; i < len(options); i += 1) { // The only option is keepalive right now @@ -57,14 +55,12 @@ export fn listen( case ip::addr6 => yield rt::AF_INET6: int; }; - const sockfd = match (rt::socket(family, rt::SOCK_STREAM, 0)) { + const sockfd = match (rt::socket(family, rt::SOCK_STREAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => yield fd; }; - let flags = rt::fcntl(sockfd, rt::F_GETFL, 0)!; - rt::fcntl(sockfd, rt::F_SETFL, flags | rt::O_CLOEXEC)!; let bk: u32 = 10; for (let i = 0z; i < len(options); i += 1) { diff --git a/net/tcp/+linux.ha b/net/tcp/+linux.ha @@ -22,14 +22,12 @@ export fn connect( case ip::addr6 => yield rt::AF_INET6: int; }; - const sockfd = match (rt::socket(family, rt::SOCK_STREAM, 0)) { + const sockfd = match (rt::socket(family, rt::SOCK_STREAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => yield fd; }; - let flags = rt::fcntl(sockfd, rt::F_GETFL, 0)!; - rt::fcntl(sockfd, rt::F_SETFL, flags | rt::O_CLOEXEC)!; for (let i = 0z; i < len(options); i += 1) { // The only option is keepalive right now @@ -57,14 +55,12 @@ export fn listen( case ip::addr6 => yield rt::AF_INET6: int; }; - const sockfd = match (rt::socket(family, rt::SOCK_STREAM, 0)) { + const sockfd = match (rt::socket(family, rt::SOCK_STREAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => yield fd; }; - let flags = rt::fcntl(sockfd, rt::F_GETFL, 0)!; - rt::fcntl(sockfd, rt::F_SETFL, flags | rt::O_CLOEXEC)!; let bk: u32 = 10; for (let i = 0z; i < len(options); i += 1) { diff --git a/net/udp/+freebsd.ha b/net/udp/+freebsd.ha @@ -19,7 +19,7 @@ export fn connect( case ip::addr6 => yield rt::AF_INET6: int; }; - const sockfd = match (rt::socket(family, rt::SOCK_DGRAM, 0)) { + const sockfd = match (rt::socket(family, rt::SOCK_DGRAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => @@ -48,14 +48,12 @@ export fn listen( case ip::addr6 => yield rt::AF_INET6: int; }; - const sockfd = match (rt::socket(family, rt::SOCK_DGRAM, 0)) { + const sockfd = match (rt::socket(family, rt::SOCK_DGRAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => yield fd; }; - let flags = rt::fcntl(sockfd, rt::F_GETFL, 0)!; - rt::fcntl(sockfd, rt::F_SETFL, flags | rt::O_CLOEXEC)!; const sockaddr = ip::to_native(addr, port); const sz = ip::native_addrlen(addr); diff --git a/net/udp/+linux.ha b/net/udp/+linux.ha @@ -19,7 +19,7 @@ export fn connect( case ip::addr6 => yield rt::AF_INET6: int; }; - const sockfd = match (rt::socket(family, rt::SOCK_DGRAM, 0)) { + const sockfd = match (rt::socket(family, rt::SOCK_DGRAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => @@ -48,14 +48,12 @@ export fn listen( case ip::addr6 => yield rt::AF_INET6: int; }; - const sockfd = match (rt::socket(family, rt::SOCK_DGRAM, 0)) { + const sockfd = match (rt::socket(family, rt::SOCK_DGRAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => yield fd; }; - let flags = rt::fcntl(sockfd, rt::F_GETFL, 0)!; - rt::fcntl(sockfd, rt::F_SETFL, flags | rt::O_CLOEXEC)!; const sockaddr = ip::to_native(addr, port); const sz = size(rt::sockaddr): u32; diff --git a/net/unix/+freebsd.ha b/net/unix/+freebsd.ha @@ -19,14 +19,12 @@ export fn connect(addr: addr) (io::file | net::error) = { case invalid => return errors::unsupported; // path too long }; - const sockfd = match (rt::socket(rt::AF_UNIX: int, rt::SOCK_STREAM, 0)) { + const sockfd = match (rt::socket(rt::AF_UNIX: int, rt::SOCK_STREAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => yield fd; }; - let flags = rt::fcntl(sockfd, rt::F_GETFL, 0)!; - rt::fcntl(sockfd, rt::F_SETFL, flags | rt::O_CLOEXEC)!; const sz = size(rt::sockaddr_un): u32; match (rt::connect(sockfd, &sockaddr, sz)) { @@ -49,14 +47,12 @@ export fn listen( case invalid => return errors::unsupported; // path too long }; - const sockfd = match (rt::socket(rt::AF_UNIX: int, rt::SOCK_STREAM, 0)) { + const sockfd = match (rt::socket(rt::AF_UNIX: int, rt::SOCK_STREAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => yield fd; }; - let flags = rt::fcntl(sockfd, rt::F_GETFL, 0)!; - rt::fcntl(sockfd, rt::F_SETFL, flags | rt::O_CLOEXEC)!; let bk: u32 = 10; for (let i = 0z; i < len(options); i += 1) { diff --git a/net/unix/+linux.ha b/net/unix/+linux.ha @@ -20,14 +20,12 @@ export fn connect(addr: addr) (io::file | net::error) = { case invalid => return errors::unsupported; // path too long }; - const sockfd = match (rt::socket(rt::AF_UNIX: int, rt::SOCK_STREAM, 0)) { + const sockfd = match (rt::socket(rt::AF_UNIX: int, rt::SOCK_STREAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => yield fd; }; - let flags = rt::fcntl(sockfd, rt::F_GETFL, 0)!; - rt::fcntl(sockfd, rt::F_SETFL, flags | rt::O_CLOEXEC)!; const sz = size(rt::sockaddr_un): u32; match (rt::connect(sockfd, &sockaddr, sz)) { @@ -50,14 +48,12 @@ export fn listen( case invalid => return errors::unsupported; // path too long }; - const sockfd = match (rt::socket(rt::AF_UNIX: int, rt::SOCK_STREAM, 0)) { + const sockfd = match (rt::socket(rt::AF_UNIX: int, rt::SOCK_STREAM | rt::SOCK_CLOEXEC, 0)) { case let err: rt::errno => return errors::errno(err); case let fd: int => yield fd; }; - let flags = rt::fcntl(sockfd, rt::F_GETFL, 0)!; - rt::fcntl(sockfd, rt::F_SETFL, flags | rt::O_CLOEXEC)!; let bk: u32 = 10; for (let i = 0z; i < len(options); i += 1) { diff --git a/net/unix/socketpair.ha b/net/unix/socketpair.ha @@ -11,7 +11,7 @@ use io; // domain and returns an unnamed pair of sockets of type [[rt::SOCK_STREAM]]. export fn socketpair() ((io::file, io::file) | net::error) = { let sv: [2]int = [0...]; - match (rt::socketpair(rt::AF_UNIX : int, rt::SOCK_STREAM : int, 0, &sv)) { + match (rt::socketpair(rt::AF_UNIX : int, (rt::SOCK_STREAM | rt::SOCK_CLOEXEC) : int, 0, &sv)) { case let err: rt::errno => return errors::errno(err); case =>