Skip to content

Commit

Permalink
Don't pass socket file descriptors to subprocesses on Unix (`SOCK_CLO…
Browse files Browse the repository at this point in the history
…EXEC`) (#14632)

Co-authored-by: Julien Portalier <julien@portalier.com>
  • Loading branch information
carlhoerberg and ysbaddaden committed May 29, 2024
1 parent fcd95b8 commit ca7aae5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions spec/std/socket/socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,11 @@ describe Socket, tags: "network" do
end
end
end

{% unless flag?(:win32) %}
it "closes on exec by default" do
socket = Socket.new(Socket::Family::INET, Socket::Type::STREAM, Socket::Protocol::TCP)
socket.close_on_exec?.should be_true
end
{% end %}
end
4 changes: 4 additions & 0 deletions src/crystal/system/unix/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module Crystal::System::Socket
alias Handle = Int32

private def create_handle(family, type, protocol, blocking) : Handle
{% if LibC.has_constant?(:SOCK_CLOEXEC) %}
# Forces opened sockets to be closed on `exec(2)`.
type = type.to_i | LibC::SOCK_CLOEXEC
{% end %}
fd = LibC.socket(family, type, protocol)
raise ::Socket::Error.from_errno("Failed to create socket") if fd == -1
fd
Expand Down

0 comments on commit ca7aae5

Please sign in to comment.