Created
September 5, 2013 20:22
-
-
Save chetan/6455643 to your computer and use it in GitHub Desktop.
Pass file descriptors (FD) between unrelated processes via a unix socket
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
# PASS FD TO ANOTHER PROCESS VIA UNIX SOCKET | |
def start_fd_listener | |
@pass_fd = UNIXServer.new("/tmp/puma_fd.sock") | |
ENV["PUMA_PASS_FD"] = @pass_fd.path | |
Thread.new { | |
begin | |
while true do | |
sock = @pass_fd.accept | |
binder.listeners.each_with_index do |(bind,io),i| | |
# puts "exporting #{bind} on FD #{io.to_i} to socket client" | |
sock.puts(bind) | |
sock.send_io(io) | |
end | |
sock.close | |
end | |
ensure | |
# cleanup fd server | |
File.unlink(@pass_fd.path) | |
end | |
} | |
end | |
# now to connect and receive the FD: | |
UNIXSocket.open(ENV["PUMA_PASS_FD"]) do |sock| | |
bind = sock.readline | |
io = sock.recv_io() | |
# add to binder | |
tcp_io = TCPServer.for_fd(io.to_i) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment