Method: Socket.unix_server_socket

Defined in:
lib/socket.rb

.unix_server_socket(path) ⇒ Object

creates a UNIX server socket on path

If no block given, it returns a listening socket.

If a block is given, it is called with the socket and the block value is returned. When the block exits, the socket is closed and the socket file is removed.

socket = Socket.unix_server_socket("/tmp/s")
p socket                  #=> #<Socket:fd 3>
p socket.local_address    #=> #<Addrinfo: /tmp/s SOCK_STREAM>

Socket.unix_server_socket("/tmp/sock") {|s|
  p s                     #=> #<Socket:fd 3>
  p s.local_address       #=> # #<Addrinfo: /tmp/sock SOCK_STREAM>
}

807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/socket.rb', line 807

def self.unix_server_socket(path)
  if !unix_socket_abstract_name?(path)
    begin
      st = File.lstat(path)
    rescue Errno::ENOENT
    end
    if st && st.socket? && st.owned?
      File.unlink path
    end
  end
  s = Addrinfo.unix(path).listen
  if block_given?
    begin
      yield s
    ensure
      s.close if !s.closed?
      if !unix_socket_abstract_name?(path)
        File.unlink path
      end
    end
  else
    s
  end
end