-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18434 from Homebrew/unix-socket
Allow sockets to use longer paths on macOS
- Loading branch information
Showing
8 changed files
with
101 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "socket" | ||
|
||
module OS | ||
module Mac | ||
# Wrapper around UNIXSocket to allow > 104 characters on macOS. | ||
module UNIXSocketExt | ||
extend T::Helpers | ||
|
||
requires_ancestor { Kernel } | ||
|
||
sig { params(path: String).returns(String) } | ||
def sockaddr_un(path) | ||
if path.bytesize > 252 # largest size that can fit into a single-byte length | ||
raise ArgumentError, "too long unix socket path (#{path.bytesize} bytes given but 252 bytes max)" | ||
end | ||
|
||
[ | ||
path.bytesize + 3, # = length (1 byte) + family (1 byte) + path (variable) + null terminator (1 byte) | ||
1, # AF_UNIX | ||
path, | ||
].pack("CCZ*") | ||
end | ||
end | ||
end | ||
end | ||
|
||
Utils::UNIXSocketExt.singleton_class.prepend(OS::Mac::UNIXSocketExt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "extend/os/mac/utils/socket" if OS.mac? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "socket" | ||
|
||
module Utils | ||
# Wrapper around UNIXSocket to allow > 104 characters on macOS. | ||
module UNIXSocketExt | ||
extend T::Generic | ||
|
||
sig { | ||
type_parameters(:U).params( | ||
path: String, | ||
_block: T.proc.params(arg0: UNIXSocket).returns(T.type_parameter(:U)), | ||
).returns(T.type_parameter(:U)) | ||
} | ||
def self.open(path, &_block) | ||
socket = Socket.new(:UNIX, :STREAM) | ||
socket.connect(sockaddr_un(path)) | ||
unix_socket = UNIXSocket.for_fd(socket.fileno) | ||
socket.autoclose = false # Transfer autoclose responsibility to UNIXSocket | ||
yield unix_socket | ||
end | ||
|
||
sig { params(path: String).returns(String) } | ||
def self.sockaddr_un(path) | ||
Socket.sockaddr_un(path) | ||
end | ||
end | ||
|
||
# Wrapper around UNIXServer to allow > 104 characters on macOS. | ||
class UNIXServerExt < Socket | ||
extend T::Generic | ||
|
||
Elem = type_member(:out) { { fixed: String } } | ||
|
||
sig { returns(String) } | ||
attr_reader :path | ||
|
||
sig { params(path: String).void } | ||
def initialize(path) | ||
super(:UNIX, :STREAM) | ||
bind(UNIXSocketExt.sockaddr_un(path)) | ||
listen(Socket::SOMAXCONN) | ||
@path = path | ||
end | ||
|
||
sig { returns(UNIXSocket) } | ||
def accept_nonblock | ||
socket, = super | ||
socket.autoclose = false # Transfer autoclose responsibility to UNIXSocket | ||
UNIXSocket.for_fd(socket.fileno) | ||
end | ||
end | ||
end | ||
|
||
require "extend/os/utils/socket" |