# frozen_string_literal: false
raise(LoadError, "UNIXServer is required") unless defined?(UNIXServer)
# Implements DRb over a UNIX socket
# DRb UNIX socket URIs look like <code>drbunix:<path>?<option></code>. The
class DRbUNIXSocket < DRbTCPSocket
if /\Adrbunix:(.*?)(\?(.*))?\z/ =~ uri
raise(DRbBadScheme, uri) unless uri.start_with?('drbunix:')
raise(DRbBadURI, 'can\'t parse uri:' + uri)
def self.open(uri, config)
filename, = parse_uri(uri)
soc = UNIXSocket.open(filename)
self.new(uri, soc, config)
def self.open_server(uri, config)
filename, = parse_uri(uri)
uri = 'drbunix:' + soc.path
soc = UNIXServer.open(filename)
owner = config[:UNIXFileOwner]
group = config[:UNIXFileGroup]
owner = Etc.getpwnam( owner ).uid if owner
group = Etc.getgrnam( group ).gid if group
File.chown owner, group, filename
mode = config[:UNIXFileMode]
File.chmod(mode, filename) if mode
self.new(uri, soc, config, true)
def self.uri_option(uri, config)
filename, option = parse_uri(uri)
return "drbunix:#{filename}", option
def initialize(uri, soc, config={}, server_mode = false)
@server_mode = server_mode
# import from tempfile.rb
tmpname = sprintf('%s/druby%d.%d', tmpdir, $$, n)
unless File.exist?(tmpname) or File.exist?(lock)
raise "cannot generate tempfile `%s'" % tmpname if n >= Max_try
soc = UNIXServer.new(tmpname)
shutdown # DRbProtocol#shutdown
path = @socket.path if @server_mode
File.unlink(path) if @server_mode
self.class.new(nil, s, @config)
DRbProtocol.add_protocol(DRbUNIXSocket)